codeetcetera
codeetcetera

Reputation: 3203

Ignoring UIGestureRecognizer when hitting button

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

Upvotes: 10

Views: 5242

Answers (2)

ChikabuZ
ChikabuZ

Reputation: 10185

In Swift:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if touch.view is UIButton {
        return false
    }
    return true
}

Upvotes: 3

jaminguy
jaminguy

Reputation: 25930

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

Upvotes: 18

Related Questions