10

I have a gesture recognizer set up so that my toolbar slides down when the screen is tapped. When I hit a button on the bar, that counts as a tap. How do I cancel the gesture in those cases?

Thanks

codeetcetera
  • 3,213
  • 4
  • 37
  • 62

2 Answers2

18

You can look at the SimpleGestureRecognizers sample project.

http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // Disallow recognition of tap gestures in the button.
    if ((touch.view == button) && (gestureRecognizer == tapRecognizer)) {
        return NO;
    }
    return YES;
}
jaminguy
  • 25,840
  • 2
  • 23
  • 21
  • The gestureRecognizer.view is being listed as the overall view, not the toolbar/buttons. – codeetcetera May 11 '11 at 14:02
  • 3
    This if statement works for ignoring every button in a toolbar. Took me a minute to find it, thought it might be worth adding. if([touch.view isDescendantOfView:_toolbar]) – codeetcetera May 12 '11 at 17:23
  • 7
    My solution was `return !([touch.view isKindOfClass[UIButton class]])` to allow events for all buttons on my view. – MrTJ Oct 19 '12 at 09:34
3

In Swift:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if touch.view is UIButton {
        return false
    }
    return true
}
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86