36
votes

How would you allow a UIGestureRecognizer of a UIView to receive a touch event but also make sure that another, underlaying/overlaying UIView also receives that very same touch event?

Lets say I have got the following view-hierachie: view hierachie scribble

Views A (blue) and B (red) are both subviews of the same superview (gray). In other words, they are both siblings and the order decides on which of them covers the other.

Option 1: View B (red) has a regular UIButton as its subview. Option 2: View A (blue) has a regular UIButton as its subview.

Given Option 1 for the sake of explanations: View A (blue) has a UITapGestureRecognizer (or maybe even more, other UIGestureRecognizers) attached. What would be the most elegant approach to make sure that the UIButton does receive all touches on its region but also the view A (blue) receives those touches on its UITapGestureRecognizer?

If possible by any means, the solution should not involve extending the UIButton-class and manually forwarding any events from the button itself. Imagine view B would contain a lot more controls and not just a button. All of those controls should somehow allow view A to receive the touches on its UIGestureRecognizer.

I did provide two options as I do not care which of those views (A or B) comes first as long as both receive the touches.

All of my approaches so far abruptly reached dead-ends. Maybe a custom responder-chain would be a nice solution? I keep thinking that there must be an elegant solution within the entire UIKit/UIEvent/UIResponder stack.

1
+1 for such a thoroughly crafted question :)Nick Weaver

1 Answers

20
votes

Have you tried with this?

cancelsTouchesInView A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.

@property(nonatomic) BOOL cancelsTouchesInView