简体   繁体   English

如何使用UISwipeGestureRecognizer将“点击”传递到UIView下面的UIButton?

[英]How to pass a 'tap' to UIButton that is underneath UIView with UISwipeGestureRecognizer?

I have a UIButton underneath a (transparent) UIView. 我在(透明)UIView下面有一个UIButton。 The UIView above has a UISwipeGestureRecognizer added to it, and that is its only purpose - to detect certain swipe gestures. 上面的UIView添加了一个UISwipeGestureRecognizer,这是它唯一的目的 - 检测某些滑动手势。 I want all other touches to be ignored by that UIView, and passed to other views (such as my UIButton underneath). 我希望UIView忽略所有其他触摸,并传递给其他视图(例如我下面的UIButton)。 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. 目前,上面的UIView似乎正在检测水龙头(例如),什么都不做(应该是这样),而不是让UIButton下面有机会做出响应。

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. 我基本上只是想知道如何告诉UIView只注意某种类型的添加手势识别器,并忽略(因此让后面的视图通过)所有其他触摸。

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? 你确定你需要那样做吗?

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. 将UISwipeGesture添加到视图,该视图是UIView和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. 然后,在处理这些滑动时,找出滑动起源的位置,以及该点是否在我曾经拥有UIView的框架中。 (As I mentioned, the only reason for the existence of the UIView was to define a target area for these swipe gestures.) (正如我所提到的,UIView存在的唯一原因是为这些滑动手势定义目标区域。)

In my case, I fixed it by not using a button, but rather a UITapGestureRecognizer. 就我而言,我通过不使用按钮来修复它,而是使用UITapGestureRecognizer。 My pan gesture recognizer was added to the background view, and the tap gesture was added to a view above it. 我的平移手势识别器已添加到背景视图中,并且轻击手势已添加到其上方的视图中。

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. 最后,您的UIButton通过UIControl继承UIView形式。 Therefore there is practically nothing that you could do with a view but not with a button. 因此,几乎没有任何东西可以用视图做,但不能用按钮做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM