UIGestureRecognizer receive touch but forward it to UIControl as well
Asked Answered
F

1

38

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.

Frulla answered 30/5, 2011 at 10:43 Comment(1)
+1 for such a thoroughly crafted question :)Skipjack
G
21

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
Gaylenegayler answered 30/5, 2011 at 10:51 Comment(3)
I did try that with both, Option 1 and 2 and using that property did not make any difference; in both cases, only the button or only the view received the touches on that button-region.Frulla
Whoops, I did actually try a different option when attempting to get cancelsTouchesInView to work. My button was a sibling of view A and B, hence the property had no effect. When using option 1 as described in my question, that property works exactly as described. Any touch onto that button triggers the button touch-up-inside as well as a touch gesture recognition on the superview of that button. That does not actually solve my entire quest (get touch recognized also onto that other view) but it gets me a big step closer to the goal - hence a +1 - ty.Frulla
example code, in my case, '-(IBAction)handleHoldOnPlay:(UIGestureRecognizer*)sender { 'sender.cancelsTouchesInView = NO;...' (thank you sergio)Rivalry

© 2022 - 2024 — McMap. All rights reserved.