如何检查一秒钟内按下 UIButton 的次数

How to check how many times UIButton pressed in One Second

我正在开发一个简单的苹果手表演示。我想在其中检查一秒内按下了多少次 UIButton
给我一些提示来回答我的问题。
这是我实现的一些代码:

- (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;
}  

随时询问我的代码。也给我一些建议。

[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(calculateClickRate)
                            userInfo:nil
                        repeats:YES];

-(void) calculateClickRate
{
 float rate = clicks;
 clicks = 0;
}

如果你像这样class在你的class中创建一个计数器和时间开始变量

var pressesInOneSecond = 1
var timeStart: NSDate?

您可以使用类似这样的方法计算按钮按下操作的次数

@IBAction func buttonWasPressed(sender: AnyObject) {
    if timeStart == nil {
        timeStart = NSDate()
    } else {        
        let end = NSDate()
        let timeInterval: Double = end.timeIntervalSinceDate(timeStart!)
        if timeInterval < 1 {            
            pressesInOneSecond++
        } else {
            // One second has elapsed and the button press count 
            // is now stored in pressesInOneSecond.
            print("presses in one second was \(pressesInOneSecond)")
        }
    }
}

按钮上的计数器可以使用

重置
pressesInOneSecond = 1
timeStart = nil

为了解释这是如何工作的,NSDate() 为您提供了当前时间,NSTimeInterval 中记录的 timeInterval 是开始时间与结束时间的衡量标准(以秒为单位)。在本例中,计数限制设置为 1 秒。第一次按下开始计数,1 秒后按下不保存。

全局变量:

int buttonTapCounts;

viewDidLoad 方法:

[self performSelector:@selector(checkCount) withObject:nil afterDelay:1.0];

UIButton 事件的方法:

-(IBAction)tapButtonClicked:(id)sender { buttonTapCounts ++; }

1 秒后检查计数器:

-(void)checkCount
{ 
     NSLog(@"Button tap clicked : %d times", buttonTapCounts);

    // Resetting the button count
    buttonTapCounts = 0;
}

感谢大家发布答案。
我使用代码解决了这个问题。
这是我的按钮点击方法:

 - (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++;
    }  

StartTimeCurrentTimedouble 变量。