iOS - 多个Tap手势识别器

时间:2012-12-13 19:02:48

标签: ios tap uitapgesturerecognizer

在我的应用程序中,我要检测单,双和三个水龙头。所以,我正在使用UITapGestureRecognizer。我正在使用以下代码:

    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGestureOnAnimal:)];
    oneTap.numberOfTapsRequired = 1;

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGestureOnAnimal:)];
    doubleTap.numberOfTapsRequired = 2;
    [doubleTap requireGestureRecognizerToFail:oneTap];

    UITapGestureRecognizer* tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTripleTapGestureOnAnimal:)];
    tripleTap.numberOfTapsRequired = 3;
    [tripleTap requireGestureRecognizerToFail:doubleTap];

    [self addGestureRecognizer:oneTap];
    [self addGestureRecognizer:doubleTap];
    [self addGestureRecognizer:tripleTap];

但问题是它始终仅检测单击和双击。它根本没有检测到三重敲击....有人可以指出我正在做的检测三重敲击的错误吗?

2 个答案:

答案 0 :(得分:17)

检查,

[oneTap requireGestureRecognizerToFail:doubleTap];
[oneTap requireGestureRecognizerToFail:tripleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];

您已经切换了方法中的水龙头,而您没有在上面的第二行。理想情况下,只有在双击和三击失败时才能检测到一个敲击。当三次敲击失败时,应检测到双击。

答案 1 :(得分:0)

将您的2个requireGestureRecognizerToFail调用更改为:

[oneTap requireGestureRecognizerToFail:tripleTap];
[oneTap requireGestureRecognizerToFail:doubleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];