UIGestureRecognizer receive touch but forward it to UIControl as well

11,346

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
Share:
11,346
mostafa tourad
Author by

mostafa tourad

Connect via Xing: https://www.xing.com/profile/Till_Toenshoff?sc_o=mxb_p Connect via LinkedIn: http://www.linkedin.com/pub/till-toenshoff/1/3aa/a48

Updated on June 03, 2022

Comments

  • mostafa tourad
    mostafa tourad almost 2 years

    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.

  • mostafa tourad
    mostafa tourad almost 13 years
    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.
  • mostafa tourad
    mostafa tourad almost 13 years
    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.
  • tmr
    tmr over 9 years
    example code, in my case, '-(IBAction)handleHoldOnPlay:(UIGestureRecognizer*)sender { 'sender.cancelsTouchesInView = NO;...' (thank you sergio)