2

For some reason, my UITapGestureRecognizer is not calling it's method when I tap a UIButton. What's really weird is that I used breakpoints in Xcode to make sure that the gestureRecognizer:shouldReceiveTouch: method is returning YES. The gesture should be calling it's method, but it isn't. I have cancelsTouchesInView set to YES, but it doesn't seem to do anything.

- (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
}

Here's the code where I set up all my gesture recognizers. Maybe the others are interfering with my tap gesture.

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];
bbraunj
  • 153
  • 2
  • 13

2 Answers2

2

A button has its own tappability, so there's a conflict between the button and the gesture recognizer. In iOS 6, button wins; the gesture recognizer is thus prevented from recognizing.

In iOS 6, there is a UIView gestureRecognizerShouldBegin: method. The UIButton returns NO (that's built-in), so that's the outcome of the conflict.

This takes even higher priority than your gestureRecognizer:shouldReceiveTouch:.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So if I wanted to stop the UIButton from having priority, I would have to subclass UIButton and override the `gestureRecognizerShouldBegin:` method. – bbraunj May 11 '13 at 02:47
  • It would be simpler to attach a tap gesture recognizer to the button itself. Then that tap gesture recognizer would win. However, this kind of kills the button's own tap; why not just use the button's built-in action message and let the button work the way it wants to? – matt May 11 '13 at 02:55
-2

Try setting the numberOfTapsRequired property of your UITapGestureRecognizer. Something like this should suffice:

tap.numberOfTapsRequired = 1;

Max

mxweas
  • 244
  • 1
  • 3