Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer on UIView and Its Subview Respond Together When Subview Is Tapped

UITapGestureRecognizer is applied to both UIImageView and its subview (UITextView). However, when I tap on subview, the receiver becomes subview and its parent view (i.e. UIImageView + UITextView). It should however be only subview because that was the one I tapped. I was assuming nested gestures would react first but apparently parent receives the fist tap and then it goes to child.

So, there are different solutions out there for various scenarios (not similar to mine but rather buttons inside scroll view conflict). How can I easily fix my issue without possible subclassing and for iOS 6+ support? I tried delaying touch on start for UIGestureRecognizer on UIImageView and I tried setting cancelsTouchesInView to NO - all with no luck.

like image 238
Vad Avatar asked Oct 25 '13 14:10

Vad


1 Answers

Try the following code:

conform the <UIGestureRecognizerDelegate> to your class.

set yourGesture.delegate = self;

then add this delegate Method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // return YES (the default) to allow the gesture recognizer to examine the touch object, NO to prevent the gesture recognizer from seeing this touch object.
    if([touch.view isKindOfClass: [UITextView class]] == YES)] {
        return YES;
    }
    else {
        return NO;
    }
}

Hope it will solve your issue. Enjoy Coding..!!!!

like image 118
Sahil Mahajan Avatar answered Oct 18 '22 11:10

Sahil Mahajan