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

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

问题描述



我有一个菜单视图,使用touchEvents从左边滑动手势时这样做与水平滑动。我不使用手势识别器,我只是使用touchesBegan,touchesMoved等等,并跟踪开始触摸位置,那些中间和完成触摸位置之间的x坐标差。它工作的很好。



但是,当在身体我有一个UITableView,触摸事件不再找到他们的方式向上到管理菜单的视图。我试图通过传递触摸到superview / nextResponder,这种工作,但我看到一个touchesBegan和2 touchesMoved调用之前,它只是停止进一步处理层次结构。



具体来说,我有一个SlideMenuView,其中有一个菜单视图和一个主体视图。在身体视图内,我有一点点UITableView。我看到(通过NSLog)触摸*方法从开始调用完成就好了。我把它们传递给nextResponder,并开始在SlideMenuView中看到它们 - 但是在2次调用touchesMoved后,他们似乎停在SlideMenuView中,但继续在我的UITableView中。



我重写了UITableView来做到这一点,但是我没有更改内部了。



我知道这必须是可能的,因为它是在Facebook应用程序。你可以在身体的任何地方滑动,这是可滚动,水平和显示菜单。我想在这里自然发生相同的事情,但它看起来好像发生了奇怪的事情,UITableView正在消耗或破坏事件。



任何人都可以帮助我有了这个?

解决方案

好的,对任何感兴趣的人来说,我发现了一个干净的解决方案。 >

苹果知道他们在做什么。回到这一天,我们曾经重写 touchesBegan ,但是他们介绍了 GestureRecognizers 。对于某些控件,例如 UIScrollView (和任何子类,如 UITableView ),他们似乎屠杀他们的事件响应系统。我错了,假设手势识别器不做任何特殊的事情,只是钩到这些 touchesBegan ,...方法 - 这是不正确的。



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



所以我做的是使用 PanGestureRecognizer 在菜单视图中将 .cancelsTouchesInView 设置为 YES 。我要避开 touchesBegan ,就像瘟疫,为任何复杂的事情写自己的手势识别器。



我希望这有助于某人像我一样拉起他们的头发!


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

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.

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.

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.

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

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 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.

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.

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天全站免登陆