我有一个 UIButton
用户必须在三秒内点击它 5 次,我试图为此实现一个方法,但如果用户点击按钮 5 我会得到正确的结果连续 3 秒计算一次,例如,如果用户点击一次并停止 2 秒,则计数器将计算第一次点击。
简而言之,我需要一种方法来检测最后五次点击并知道点击是否在三秒内...
这是我的旧代码:
-(void)btnClicked{
counter++;
if (totalTime <=3 && counter==5) {
NSLog(@"My action");
// My action
}}
我知道我的代码太简单了,所以才问你亲的
最佳答案
尝试适本地改变这个例子:
// somewhere in the initialization - counter is an int, timedOut is a BOOL
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;
}
关于iphone - 我需要一种方法来检测我的 UIButton 上的最后五次点击时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12880166/