Search code examples
iosxcodeuibuttoncounterapple-watch

How to check how many times UIButton pressed in One Second


I am Developing a simple apple watch demo. In which I want to check How many times UIButton pressing in One Second.
Give me some hint for my question.
Here are some code which I implemented:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    [btnBell setBackgroundImage:[UIImage imageNamed:@"Bell.png"]];
    [self performSelector:@selector(checkCount) withObject:nil afterDelay:1.0];
    NSLog(@"%@ awakeWithContext", self);
    self.counter = 0;
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    NSLog(@"%@ will activate", self);
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    NSLog(@"%@ did deactivate", self);
}

#pragma mark - Button actions

- (IBAction)saveCounter {
    //Send count to parent application
    self.counter++;
    NSString *counterString = [NSString stringWithFormat:@"%d", self.counter];
    NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[counterString] forKeys:@[@"counterValue"]];

    //Handle reciever in app delegate of parent app
    [WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
        [self showSaveNotificationLabel];
    }];
}

#pragma mark - Helper methods

-(void)showSaveNotificationLabel {
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"beep1" ofType: @"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    myAudioPlayer.volume = 0.1;

    [myAudioPlayer play];
}
-(void)checkCount
{
    NSLog(@"Button tap clicked : %d times", self.counter);
    self.counter = 0;
}  

Feel free to ask regarding my code. and also give me some suggestion.


Solution

  • Thanks for Posting answers guys.
    I resolved this issue using the code.
    Here, is my button click method:

     - (IBAction)saveCounter {
            [myAudioPlayer play];
            //Send count to parent application
            CurrentTime = [[NSDate date] timeIntervalSince1970] * 1000;
            NSLog(@"Current Time: %f",CurrentTime);
    
            if ((CurrentTime - StartTime) < 500)
            {
                myAudioPlayer.volume = myAudioPlayer.volume +0.1;
            }
            else
            {
                myAudioPlayer.volume = 0.5;
            }
            StartTime = CurrentTime;
    
            //self.counter++;
        }  
    

    The StartTime and CurrentTime are the double variable.