如何通过“水龙头”?到带有 UISwipeGestureRecognizer 的 UIView 下方的 UIButton?
我在(透明)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否设置:
允许将触摸以及手势发送到视图层次结构?
此外,确保顶部的视图是:
我已经非常成功地将手势附加到父视图,而无需在顶部为手势创建透明子视图。您确定需要这样做吗?
Have you set:
to allow the touches to be sent to the view hierarchy as well as the gesture?
Additionally, ensure that the view on top is:
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?
昨天我一定很害怕——今天我醒来时发现了一个简单的解决方案。将 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.)
您不能将按钮放在视图顶部并向该按钮添加手势识别器吗?
最后,你的 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.
就我而言,我通过不使用按钮而是使用 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.