I have a view (let’s say View B) positioned on top of another view (View A). I would like to make some animation when a touch tap happens outside of view B.
So far I have attached taptouch gesture recognizer to View A. The problem is that View A is intercepting tap event even for the one that happens on view B (while it is overlapping View A).
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(animateViewB)];
recognizer.delegate = self;
[self.viewA addGestureRecognizer:recognizer];
How should I proceed to make sure the tap events are not sent to View A when clicking inside View B? If I check the positioning of the event relatively to View B and if I detect it is inside it. How can I make sure all events are forwarded to View B?
The reason your viewA was picking up the interaction even though you were touching viewB is that UIImageViews natively have user interaction disabled.
If you want to intercept the interaction on a UIImageView you have to set:
If you add a gesture recognizer to a view that view will be the only one that uses the particular recognizer.
The highest level view (i.e. the one on top) with its user interaction enabled will receive your touch or gesture. Views with user interaction disabled will not receive any touch or gesture events.
For a better understanding of view hierarchy you should read: View Programming Guide for iOS