Disable receiving touches from parent view on subview

Ser Pounce picture Ser Pounce · May 25, 2012 · Viewed 9.3k times · Source

I have a UIView parentView which implements a UITapGuestureRecognizer and does something when tapped. parentView has a sub view called childView which also implements a UITapGuestureRecognizer and does something when tapped.

There is an instance when I have to turn off the childViews UITapGestureRecognizer during an animation for a slight moment, and I noticed when it is turned off and I tap childView, the tap gets intercepted by parentView. Also, I have a toolbar attached to the top of this view that doesn't have any gesture recognizer attached to it, and it's touches get passed parentView (the buttons will barely work). I'm wondering is it possible to disable this without having a reference to the parents UITapGestureRecognizer?

I've tried using the exclusiveTouches property of UIView set to yes and it doesn't work. Any suggestions would be appreciated.

Answer

Ser Pounce picture Ser Pounce · Jun 24, 2012

In the parent gesture recognizers, implement the UIGestureRecognizerDelegate, and implement the following method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{
    if ([touch.view isKindOfClass:[ClassThatYouWantTouchesBlocked class]]) 
    {
        return NO;
    }
    else 
    {
        return YES; 
    }
}

Replace ClassThatYouWantTouchesBlocked with the class that you want its touches to be ignored.