UIButton inside a view that has a UITapGestureRecognizer UIButton inside a view that has a UITapGestureRecognizer ios ios

UIButton inside a view that has a UITapGestureRecognizer


You can set your controller or view (whichever creates the gesture recognizer) as the delegate of the UITapGestureRecognizer. Then in the delegate you can implement -gestureRecognizer:shouldReceiveTouch:. In your implementation you can test if the touch belongs to your new subview, and if it does, instruct the gesture recognizer to ignore it. Something like the following:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {    // test if our control subview is on-screen    if (self.controlSubview.superview != nil) {        if ([touch.view isDescendantOfView:self.controlSubview]) {            // we touched our control surface            return NO; // ignore the touch        }    }    return YES; // handle the touch}


As a follow up to Casey's follow up to Kevin Ballard's answer:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {        if ([touch.view isKindOfClass:[UIControl class]]) {            // we touched a button, slider, or other UIControl            return NO; // ignore the touch        }    return YES; // handle the touch}

This basically makes all user input types of controls like buttons, sliders, etc. work


Found this answer here: link

You can also use

tapRecognizer.cancelsTouchesInView = NO;

Which prevents the tap recognizer to be the only one to catch all the taps

UPDATE - Michael mentioned the link to the documentation describing this property: cancelsTouchesInView