UITapGestureRecognizer没有调用它的target-action方法

时间:2013-05-10 23:37:10

标签: ios objective-c uibutton uigesturerecognizer uitapgesturerecognizer

出于某种原因,当我点击UIButton时,我的UITapGestureRecognizer并没有调用它的方法。真正奇怪的是我在Xcode中使用了断点来确保gestureRecognizer:shouldReceiveTouch:方法返回YES。手势应该是调用它的方法,但事实并非如此。我将cancelsTouchesInView设置为YES,但它似乎没有做任何事情。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass: [UIButton class]] && !editingTaskName)
        return NO;
    else if ([touch.view isKindOfClass: [UITextField class]])
        return NO;

    return YES; // handle the touch
}

这是我设置所有手势识别器的代码。也许其他人正在干扰我的轻拍手势。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(longPress:)];
[longPress setMinimumPressDuration: 0.3];
[longPress setDelaysTouchesBegan: YES];

[self setLongPressGesture: longPress];
[[self tableView] addGestureRecognizer: longPress];

UITapGestureRecognizer *backToTableView = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(backTapRecognized:)];
[backToTableView setCancelsTouchesInView: YES];
[backToTableView setDelegate: self];
[backToTableView setEnabled: NO];

[self setBackTapGesture: backToTableView];
[[self tableView] addGestureRecognizer: backToTableView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideShowEditing:)];
[tap setCancelsTouchesInView: YES];
[tap setDelegate: self];

[self setEditTap: tap];
[[self tableView] addGestureRecognizer: tap];

2 个答案:

答案 0 :(得分:1)

按钮具有自己的可插拔性,因此按钮和手势识别器之间存在冲突。在iOS 6中,按钮获胜;因此,手势识别器无法识别。

在iOS 6中,有一个UIView gestureRecognizerShouldBegin:方法。 UIButton返回NO(内置),这就是冲突的结果。

这比gestureRecognizer:shouldReceiveTouch:更优先。

答案 1 :(得分:-2)

尝试设置UITapGestureRecognizer的numberOfTapsRequired属性。这样的事情就足够了:

tap.numberOfTapsRequired = 1;

最高