iphone - 过滤单击和双击

标签 iphone objective-c ios touch taps

当用户单击我的 View 时,我需要一个特定的方法来运行。 当用户双击时,我需要另一种方法发生。

问题是双击会触发单击,这会在我的逻辑中引入错误。 我不能使用 UIGestureRecognizer,因为我需要跟踪这些点。

我尝试了一些 bool 值,但没有机会。我也尝试了 cancel/perfomSelector-delay 技术,但它不起作用(这很奇怪,因为其他论坛上的其他人说它有效,也许模拟器触摸检测不同?)

我试图让用户设置一 block 棋子的位置(拖动、旋转),但我需要注意棋子的交叉点、剪切到棋盘区域等,这就是为什么一个简单的 bool 值无法解决的原因问题。

提前致谢!

最佳答案

检查 this , 似乎是您要找的东西:

Below the code to use UITapGestureRecognizer to handle single tap and double tap. The code is designed that it won't fire single tap event if we got double tap event. The trick is use requireGestureRecognizerToFail to ask the single tap gesture wait for double tap event failed before it fire. So when the user tap on the screen, the single tap gesture recognizer will not fire the event until the double tap gesture recognizer. If the double tap gesture recognizer recognize the event, it will fire a double tab event and skip the single tap event, otherwise it will fire a single tap event.

    UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                            initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTapGestureRecognizer.numberOfTapsRequired = 2;

    //tapGestureRecognizer.delegate = self;
    [self addGestureRecognizer:doubleTapGestureRecognizer];

    //
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGestureRecognizer.numberOfTapsRequired = 1;

    [singleTapGestureRecognizer requireGestureRecognizerToFail: doubleTapGestureRecognizer];
    //tapGestureRecognizer.delegate = self;
    [self addGestureRecognizer:singleTapGestureRecognizer];

可能我不明白你所说的“我需要跟踪点”是什么意思,但我认为使用手势识别器执行此操作没有任何问题。

关于iphone - 过滤单击和双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6587453/

相关文章:

iOS 错误 - CGContextRestoreGState : invalid context 0x0

objective-c - 我想实现一半 UITableView 一半 MapView UIView,中间有一个按钮

objective-c - 什么是 "pushing" View Controller ?

ios - 圆形渐变色

ios - 展开时 ImageView 发现为零

iphone - 对于给定的配置,我应该尝试使用 NSUserDefaults 还是去 CoreData?

ios - 使用 Swift 检查为表格 View 单元格中的文本字段选择的行?

iphone - iPhone 上的密码文本框

ios - 如何在 Xcode 中对 10000 行的 TableView 进行 UI 测试?

iphone - 自动续订订阅错误