停止 UITableView 消耗触摸事件,以便父视图中的滑动菜单可以检测水平滑动 [英] Stop UITableView consuming touch events so sliding menu in parent view can detect horizontal swipes

查看:20
本文介绍了停止 UITableView 消耗触摸事件,以便父视图中的滑动菜单可以检测水平滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了实现如此简单的事情,我快要绝望了.

I am about driven to despair trying to achieve something so simple.

我有一个带有菜单的视图,当它通过水平滑动做出手势时,使用 touchEvents 从左侧滑动.我不使用手势识别器,我只是使用 touchesBegan、touchesMoved 等,并跟踪起始触摸位置、中间位置和结束触摸位置之间的 x 坐标差异.效果很好.

I have a view with a menu that, using touchEvents slides from the left when it is gestured to do so with a horizontal swipe. I don't use gesture recognisers, I simply use the touchesBegan, touchesMoved, etc.. and track the x coordinate difference between the starting touch position, those in-between and the finishing touch position. It works great.

但是,当我在 body 中有一个 UITableView 时,触摸事件不再沿着层次结构向上找到管理菜单的视图.我试图通过将 touches 传递给 superview/nextResponder 来解决这个问题,这种方法是有效的,但我看到一个 touchesBegan 和 2 个 touchesMoved 调用,然后它就停止在层次结构中进一步处理.

But, when in the body I have a UITableView, the touch events no longer find their way up the hierarchy to the view managing the menu. I tried to get around this by passing touches to superview/nextResponder, which sort of works, but I see a touchesBegan and 2 touchesMoved calls before it simply stops being processed further up the hierarchy.

具体来说,我有一个 SlideMenuView,其中有一个菜单视图和一个主体视图.在 body 视图中,我在某个时候有 UITableView.我看到(通过 NSLog)从头到尾调用 touches* 方法就好了.我将它们传递给 nextResponder 并开始在 SlideMenuView 中看到它们 - 但在两次调用 touchesMoved 之后,它们似乎在 SlideMenuView 中停止,但在我的 UITableView 中继续.

Specifically, I have a SlideMenuView, in which there is a menu view and a body view. Inside the body view, I have at some point the UITableView. I see (via NSLog) that the touches* methods are called from start to finish just fine. I pass them on to nextResponder and begin to see them in SlideMenuView also - but after 2 calls to touchesMoved, they seem to stop in SlideMenuView but continue in my UITableView.

我正在覆盖 UITableView 来执行此操作,但我没有对其内部进行任何更改.

I am overriding the UITableView to do this, but I have changed nothing more inside of it.

我知道这一定是可能的,就像在 Facebook 应用中一样.你可以在身体的任何地方滑动,它是可滚动的、水平的并显示菜单.我希望同样的事情在这里自然发生,但看起来好像发生了一些奇怪的事情,并且 UITableView 正在消耗或破坏事件.

I know that this must be possible, as it is in the Facebook app. You can swipe anywhere in the body, which is scrollable, horizontally and reveal the menu. I want the same thing to naturally occur here, but it appears as though something weird is happening, and the UITableView is consuming or destroying the events.

有人可以帮我解决这个问题吗?一定有人以一种干净的方式实现了这一点.

Can anybody help me out with this? Somebody must have achieved this in a clean way.

推荐答案

好的,对于任何感兴趣的人 - 我找到了一个干净的解决方案.

Okay, for anybody interested - I found a clean solution.

Apple 知道他们在做什么.回到过去,我们过去常常覆盖 touchesBegan,...但后来他们引入了 GestureRecognizers.对于某些控件,例如 UIScrollView(以及任何子类,例如 UITableView),它们似乎扼杀了它们的事件响应器系统.假设 Gesture Recognizers 没有做任何特别的事情,而只是简单地连接到这些 touchesBegan、...方法 - 这是不正确的.

Apple know what they're doing. Back in the day we used to override touchesBegan, ... but then they introduced GestureRecognizers. For some controls, such as the UIScrollView (and any subclasses, such as UITableView), they seem to butcher their event responder system. I was at fault for assuming that the Gesture Recognizers don't do anything special, and simply hook in to these touchesBegan, ... methods - this is not correct.

最安全的方法是使用手势识别器.具体来说,它们有一些选项可以发挥作用.例如 .cancelsTouchesInView = YES 在我的情况下.

The safest way is to use the Gesture Recognizers. Specifically, there are some options on them that can make all the difference. Such as .cancelsTouchesInView = YES in my case.

所以我所做的是在菜单视图上使用 PanGestureRecognizer,将 .cancelsTouchesInView 设置为 YES,它就可以工作了.我将避免 touchesBegan,......就像瘟疫一样,并为任何复杂的事物编写自定义手势识别器.

So what I did, is use a PanGestureRecognizer on the Menu View, set .cancelsTouchesInView to YES, and it works. I am going to avoid touchesBegan, ... like the plague, and write custom gesture recognisers for anything complex.

我希望这能帮助像我一样拔头发的人!

I hope this helps somebody pulling their hair out like I was !

这篇关于停止 UITableView 消耗触摸事件,以便父视图中的滑动菜单可以检测水平滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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