如何通过“水龙头”?到带有 UISwipeGestureRecognizer 的 UIView 下方的 UIButton?

发布于 2024-12-01 21:47:23 字数 308 浏览 0 评论 0原文

我在(透明)UIView 下方有一个 UIButton。上面的 UIView 添加了一个 UISwipeGestureRecognizer,这是它的唯一目的 - 检测某些滑动手势。我希望该 UIView 忽略所有其他触摸,并传递给其他视图(例如下面的 UIButton)。目前,上面的 UIView 似乎正在检测点击(例如),不执行任何操作(应该如此),并且不让下面的 UIButton 有机会响应。

如果可能的话,我不想实现自己的滑动识别器。有什么解决方案/建议吗?我基本上只是想知道如何告诉 UIView 仅注意某种类型的添加手势识别器,并忽略(从而让后面的视图通过)所有其他触摸。

I have a UIButton underneath a (transparent) UIView. The UIView above has a UISwipeGestureRecognizer added to it, and that is its only purpose - to detect certain swipe gestures. I want all other touches to be ignored by that UIView, and passed to other views (such as my UIButton underneath). Currently, the UIView above seems to be detecting the tap (for example), doing nothing (as it should be), and not letting the UIButton underneath get a chance to respond.

I would prefer not to implement my own swipe recognizer, if possible. Any solutions / advice? I basically just want to know how to tell a UIView to pay attention to only a certain type of added gesture recognizer, and ignore (and thus let through to views behind) all other touches.

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

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

发布评论

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

评论(4

黎歌 2024-12-08 21:47:23

您是否设置:

mySwipeGesture.cancelsTouchesInView = NO;

允许将触摸以及手势发送到视图层次结构?

此外,确保顶部的视图是:

theTransparentView.opaque = NO;
theTransparentView.userInteractionEnabled = YES;

我已经非常成功地将手势附加到父视图,而无需在顶部为手势创建透明子视图。您确定需要这样做吗?

Have you set:

mySwipeGesture.cancelsTouchesInView = NO;

to allow the touches to be sent to the view hierarchy as well as the gesture?

Additionally, ensure that the view on top is:

theTransparentView.opaque = NO;
theTransparentView.userInteractionEnabled = YES;

I've had pretty good success attaching gestures to the parent view without needing to create a transparent subview on top for the gesture. Are you sure you need to do that?

救星 2024-12-08 21:47:23

昨天我一定很害怕——今天我醒来时发现了一个简单的解决方案。将 UISwipeGesture 添加到视图中,该视图是 UIView 和 UIButton 的超级视图。然后,在处理这些滑动时,找出滑动的起始位置,以及该点是否位于我曾经拥有 UIView 的框架中。 (正如我提到的,UIView 存在的唯一原因是为这些滑动手势定义目标区域。)

I must have just been in a funk yesterday - I woke up with a simple solution today. Add the UISwipeGesture to a view which is a superview to both the UIView and the UIButton. Then, when processing those swipes, figure out where the swipe originated, and whether that point is in the frame of where I used to have the UIView. (As I mentioned, the only reason for the existence of the UIView was to define a target area for these swipe gestures.)

难以启齿的温柔 2024-12-08 21:47:23

您不能将按钮放在视图顶部并向该按钮添加手势识别器吗?

最后,你的 UIButton 通过 UIControl 继承了 UIView。因此,除了按钮之外,您实际上无法使用视图执行任何操作。

Can't you put your button on top of the view and add gesture recognisers to that button too?

In the end, your UIButton inherits form UIView via UIControl. Therefore there is practically nothing that you could do with a view but not with a button.

一个人练习一个人 2024-12-08 21:47:23

就我而言,我通过不使用按钮而是使用 UITapGestureRecognizer 来修复它。我的平移手势识别器已添加到背景视图中,并且点击手势已添加到其上方的视图中。

In my case, I fixed it by not using a button, but rather a UITapGestureRecognizer. My pan gesture recognizer was added to the background view, and the tap gesture was added to a view above it.

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