UIGestureRecognizer接收触摸,但也将其转发给UIControl [英] UIGestureRecognizer receive touch but forward it to UIControl as well

查看:115
本文介绍了UIGestureRecognizer接收触摸,但也将其转发给UIControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

让我说我有以下观点 - hierachie :

Lets say I have got the following view-hierachie:

views A(蓝色)和B(红色)都是同一超视图(灰色)的子视图。换句话说,他们都是兄弟姐妹,订单决定哪一个涵盖另一个。

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.

选项1:
视图B(红色)有一个常规 UIButton 作为其子视图。
选项2:
视图A(蓝色)有一个常规 UIButton 作为其子视图。

Option 1: View B (red) has a regular UIButton as its subview. Option 2: View A (blue) has a regular UIButton as its subview.

为了解释,给出选项1:
视图A(蓝色)附加了 UITapGestureRecognizer (或者甚至更多,其他UIGestureRecognizers)。什么是最优雅的方法来确保 UIButton 确实接收到其区域的所有触摸,但视图A(蓝色)接收其<$ c $上的那些触摸c> UITapGestureRecognizer ?

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?

如果可能的话,解决方案不应涉及扩展 UIButton -class并手动转发按钮本身的任何事件。想象一下,视图B将包含更多控件,而不仅仅是一个按钮。所有这些控件都应该以某种方式允许视图A接收其 UIGestureRecognizer 上的触摸。

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.

我确实提供了两个选项因为我不关心这些观点(A或B)中的哪一个首先出现,只要两个都接受了触摸。

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.

到目前为止,我的所有方法都突然达到死胡同。也许自定义响应链是一个很好的解决方案?我一直认为在整个 UIKit / UIEvent / UIResponder中必须有一个优雅的解决方案 stack。

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.

推荐答案

你试过这个


cancelsTouchesInView
一个布尔值,用于影响在识别手势时是否将触摸传递给视图。

cancelsTouchesInView A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.



@property(nonatomic) BOOL cancelsTouchesInView

这篇关于UIGestureRecognizer接收触摸,但也将其转发给UIControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆