iphone - 我需要一种方法来检测我的 UIButton 上的最后五次点击时间

标签 iphone objective-c ios uibutton nstimer

我有一个 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/

相关文章:

objective-c - 如何使用kiwi测试函数调用的参数?

ios - NSArray 元素无法匹配 mapView.annotations 循环中的 Swift 数组元素类型

ios - 使用 iOS SDK 将大视频上传到 Facebook

ios - 代码调试: Nil object is not Nil

iphone - 如何在 viewController 中使用 UITabbar?

iphone - UIControlEvents 和子类化 UIControl

objective-c - OS X Web View 应用程序 - 不会加载本地 .html 网页

ios - 如果第一个通知在 swift 中被忽略,则安排第二个通知(或者如果通知被忽略则完成任何操作)

iphone - resourcePath 不工作 Xcode

ios - NSMutableDictionary 的 NSMutableArray 不显示数据