过滤单和双击 [英] filtering single and double taps

查看:137
本文介绍了过滤单和双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When the user single taps my view, i need one specific method to run. When the user double taps, i need another method do take place.

问题是双击触发单击,它会引入错误我的逻辑。
我无法使用UIGestureRecognizer因为我需要跟踪积分。

The problem is that the double tap triggers the single tap, and it introduce bugs in my logic. I can't use UIGestureRecognizer because i need to keep track of the points.

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

I try some booleans, but no chance. I also tried the cancel/perfomSelector-delay technique, but it does not work (that's strange because other folks on other forums said it works, maybe the simulator touch detection is different ?)

我试图让用户设置板块的位置(拖动,旋转),但是我需要知道片段交叉点,剪切到板区域等,这就是为什么一个简单的布尔值无法解决的原因问题。

I'm trying to let the user set the position (drag, rotate) of a board piece, but i need to be aware of piece intersections, clip to the board area, etc, that's why a simple boolean will not solve the problem.

提前致谢!

推荐答案

检查这个,似乎是正确的您正在寻找:

Check this, seems right what you are looking for:


使用UITapGestureRecognizer处理单击和双击的代码下方。该代码被设计为如果我们获得双击事件则不会触发单击事件。诀窍是使用requireGestureRecognizerToFail来询问单击手势等待双击事件在其触发之前失败。因此,当用户点击屏幕时,单击手势识别器将不会触发事件直到双击手势识别器。如果双击手势识别器识别出该事件,它将触发双标签事件并跳过单击事件,否则将触发单击事件。

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];

可能我不明白你的意思是我需要跟踪积分 ,但是我没有看到使用手势识别器执行此操作时出现任何问题。

Possibly I don't understand exactly what you mean by "i need to keep track of the points", but I don't see any problems with doing this with a gesture recognizer.

这篇关于过滤单和双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆