检测最后五次点击UIbutton的时间

要实现的功能是:用户在三秒钟内点击五次UIButton。我已经实现的是,如果用户连续点击就能返回正确值。但是如果点击一下,然后两秒后点击另外两次,计数器只会记录第一下。

怎么检测用户是否在三秒钟内点击了五次?谢谢

这是我的代码:

-(void)btnClicked{
 counter++;

if (totalTime <=3 && counter==5) {

        NSLog(@"My action");

}}

废话不说,上代码:

counter = 0;
timedOut = NO;

- (void)buttonClicked:(UIButton *)btn
{
    if ((++counter >= 5) && !timedOut) {
        NSLog(@"User clicked button 5 times within 3 secs");

        // for nitpickers
        timedOut = NO;
        counter = 0;
    }
}

// ...

[NSTimer scheduledTimerWithTimeInterval:3.0
    target:self
    selector:@selector(timedOut)
    userInfo:nil
    repeats:NO
];

- (void)timedOut
{
    timedOut = YES;
}

我的代码可能帮你

int counter = 0;
BOOL didTimeOut = NO;

- (void)buttonClicked:(UIButton *)button {
    counter ++;
    if (counter == 1) {
        didTimeOut = NO;
        [NSTimer scheduledTimerWithTimeInterval:3.0f
                                         target:self
                                       selector:@selector(timedOut)
                                       userInfo:nil
                                        repeats:NO
         ];
    } else {
        if ((counter >= 5) && !didTimeOut) {


            counter = 0;
            didTimeOut = NO;
        }
    }

}

- (void)timedOut {
    didTimeOut = YES;
}