Saturday, 8 June 2013

UIKit: Stop Touches in a Subview being Handled by its Superview

If view A has a gesture recognizer, and this view has a subview B which does not have gesture recognizer, the gesture happened in the subview B will be recognized by the gesture recognizer of view A.

This may not be what you want. For example, if subview B is showing an introduction, you do not want a tap in it to be handled by view A.

One solution is to make view A a delegate of its gesture recognizer, and define a method:

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceiveTouch:(UITouch *)touch {
    return touch.view == self;
}

In this way, the gesture recognizer will not recognize gestures happen in its subview.

No comments:

Post a Comment