UIGestureRecognizer 接收触摸但也将其转发给 UIControl

发布于 2024-11-10 14:21:24 字数 907 浏览 2 评论 0原文

如何允许 UIViewUIGestureRecognizer 接收触摸事件,同时确保另一个底层/覆盖 UIView 也接收到该事件相同的触摸事件?

假设我有以下视图层次结构: view hierachie scribble

视图 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:
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-11-17 14:21:24

您是否尝试过这个

取消TouchesInView
一个布尔值,影响识别手势时是否将触摸传递到视图。

@property(nonatomic) BOOL cancelsTouchesInView

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文