UIGestureRecognizer 接收触摸但也将其转发给 UIControl
如何允许 UIView
的 UIGestureRecognizer
接收触摸事件,同时确保另一个底层/覆盖 UIView
也接收到该事件相同的触摸事件?
假设我有以下视图层次结构:
视图 A(蓝色)和 B(红色)都是同一超级视图(灰色)的子视图。换句话说,他们都是兄弟姐妹,顺序决定了他们中的一个覆盖另一个。
选项 1: 视图 B(红色)有一个常规的 UIButton 作为其子视图。 选项2: 视图 A(蓝色)有一个常规 UIButton
作为其子视图。
为了解释起见,给出选项 1: 视图 A(蓝色)附加了一个 UITapGestureRecognizer
(或者甚至更多,其他 UIGestureRecognizer)。确保 UIButton 确实接收其区域上的所有触摸以及视图 A(蓝色)接收其 UITapGestureRecognizer 上的这些触摸的最优雅的方法是什么?
如果可能的话,解决方案不应涉及扩展 UIButton 类并手动转发按钮本身的任何事件。想象一下视图 B 将包含更多控件而不仅仅是一个按钮。所有这些控件都应该以某种方式允许视图 A 接收对其 UIGestureRecognizer
的触摸。
我确实提供了两个选项,因为我不在乎这些视图(A 或 B)中哪一个先出现,只要两者都收到触摸即可。
到目前为止,我所有的方法都突然进入了死胡同。也许自定义响应链是一个不错的解决方案?我一直认为整个 UIKit
/UIEvent
/UIResponder
堆栈中一定有一个优雅的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过这个?
Have you tried with this?