UIGestureRecognizer receive touch but forward it to UIControl as well

◇◆丶佛笑我妖孽 提交于 2019-12-20 10:18:37

问题


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:

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:


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


来源:https://stackoverflow.com/questions/6175255/uigesturerecognizer-receive-touch-but-forward-it-to-uicontrol-as-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!