Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable receiving touches from parent view on subview

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.

like image 273
Ser Pounce Avatar asked May 25 '12 06:05

Ser Pounce


2 Answers

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.

like image 94
Ser Pounce Avatar answered Sep 22 '22 06:09

Ser Pounce


What if never turn off the ChildView's tap gesture recognizer? if it is animating return from the child's tap gesture method without doing anything.

If it is an imageView than isAnimating property might come in use.

like image 37
Warif Akhand Rishi Avatar answered Sep 21 '22 06:09

Warif Akhand Rishi