当您使用非典型的嵌套 UITableViews 设置时 - 您有一个外部垂直 UITableView 托管 90° 旋转的 UITableViews (请参阅:寻找 UI 库在 iOS 中水平呈现数据):
有没有办法让iOS同时处理垂直和水平触摸?
我发现iOS对于触摸的处理非常巧妙:
水平触摸使相关的水平 UITableView 滚动,而垂直滑动使外部 UITableView 滚动。完美的。
只是,我希望能够对角移动手指并同时看到外部 UITableView 和内部 UITableView 滚动。
我尝试了几种方法(使用 canCancelContentTouches、delaysContentTouches 和触摸消息),但我还没有找到实现此目的的方法。
编辑:
下面是一个显示此行为的 XCode4 项目: http://marcanton.io/other/stackoverflow/nestedtableviews .zip
编辑:
我将此问题提交给Apple开发者技术支持,他们的回复如下:
感谢您写信给 Apple
全球开发者技术支持。
我正在回复您的询问
关于嵌入式中的触摸事件
UITableViews。
通常这是一种方法
不推荐。问题是
UITableView继承自UIScrollView
正如文档中所述
UIScrollView:
“重要提示:您不应嵌入
UIWebView 或 UITableView 对象
UIScrollView 对象。如果你这样做,
可能会导致意外的行为,因为
两个对象的触摸事件可以
混淆并错误处理。”
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html%23//apple_ref/occ/cl/UIScrollView
所以这一次,没有
让两者滚动的解决方法
同时。
我建议您提交一份
增强请求位于
http://developer.apple.com/bugreporter/
详细说明您想看到的内容
我们会在未来的版本中添加。
尽管如此,我认为必须有一种方法来启用此功能,尽管我知道不建议这样做。事实上,Apple 甚至不建议将 UITableView 托管在另一个 UITableView 中,但除了上面的例外,它的工作原理非常漂亮。
我将根据我们的集体发现不断更新这个问题。
编辑:实际上有一种方法,详细信息如下: http://marcanton.io/blog/嵌套正交表视图/
When you're working with an atypical nested UITableViews setup - where you have an outer vertical UITableView that hosts 90° rotated UITableViews (see: Looking for a UI library to present Data horizontaly in iOS ):
is there a way to make iOS process vertical and horizontal touches at the same time?
I found that iOS is very clever in processing touches:
horizontal touches make the relevant horizontal UITableView scroll, while a vertical swipe makes the outer UITableView scroll. Perfect.
Only, I'd love to be able to move my finger diagonally and see the outer UITableView and the inner UITableView scroll at the same time.
I tried a few approaches (playing with canCancelContentTouches, delaysContentTouches, and touch messages) but I haven't found a way to make this happen.
EDIT:
Here's a XCode4 project that shows this behavior: http://marcanton.io/other/stackoverflow/nestedtableviews.zip
EDIT:
I submitted this issue to Apple Developer Technical Support, here's their reply:
Thank you for writing to Apple
Worldwide Developer Technical Support.
I am responding to your inquiry
concerning touch events in embedded
UITableViews.
Typically this is an approach that is
not recommended. The issue is that
UITableView inherits from UIScrollView
and as stated in the documentation for
UIScrollView:
"Important: You should not embed
UIWebView or UITableView objects in
UIScrollView objects. If you do so,
unexpected behavior can result because
touch events for the two objects can
be mixed up and wrongly handled."
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html%23//apple_ref/occ/cl/UIScrollView
So that this time, there is not a
workaround for getting both to scroll
at the same time.
I recommend that you file an
enhancement request at
http://developer.apple.com/bugreporter/
detailing what you would like to see
us add in a future release.
Still, I think that there has to be a way to enable this functionality, although I understand that this is not recommended. In fact, Apple does not even recommend hosting UITableViews inside another UITableView, but with the exception made above, it works quite beautifully.
I'll keep this question updated with our collective findings.
EDIT: There actually is a way, detailed here: http://marcanton.io/blog/nested-orthogonal-tableviews/
发布评论
评论(2)
这必须是拦截的触摸事件的自定义镜像。触摸事件遵循响应者链模型,这意味着如果响应者链(最顶层(最外层)视图)中的某个对象无法处理该事件或操作,它会将消息重新发送给下一个响应者(本例中为后台 UITableView 在链中)。这就是为什么您会看到水平事件转到水平
UITableView
而垂直事件转到垂直UITableView
。对角线触摸事件具有适用的水平和垂直事件,因此最顶层视图(外部垂直 UITableView)可以响应垂直触摸并吞噬该事件。如果您考虑一下,所有垂直触摸都可能有一点水平事件(想想当您轻弹手指时),因此可能在后台完成一些工作来确定如何解释触摸事件(或者作为垂直触摸)或水平)。
我发现了这个 tread 将事件向下传递到响应者链中的下一个对象。您可能想尝试一下,作为您难题的部分解决方案。剩下的就是弄清楚如何捕获水平触摸事件并将其传递给下一个响应者。
This would have to be a custom mirroring of intercepted touch events. Touch events follow the responder chain model, which means that if an object in the responder chain (the top most (outermost) view) cannot handle the event or action, it resends the message to the next responder (in this case the background
UITableView
in the chain). This is why you are seeing the horizontal events go to the horizontalUITableView
and the vertical events going to the verticalUITableView
. A diagonal touch event has applicable horizontal and vertical events, so the top-most view (the outer vertical UITableView) can respond to the vertical touches and swallows the event.If you think about it, all vertical touches likely have a little bit of horizontal events (think about when you flick your finger), so there is likely some work done in the background to determine how to interpret the touch event (either as a vertical or horizontal).
I found this tread on passing events down to the next object in the responder chain. You might want to give this a try as a partial solution to your puzzle. The rest is to figure out how to capture the horizontal touch events and pass them along to the next responder.
有趣的是,我还没有尝试过这种设置,但我会尝试拦截嵌套 UITableView 上的触摸事件并将任何垂直移动委托给外部 UITableView - 反之亦然。
Interesting, I haven't played around with this kind of setup yet, but I would try to intercept touch events on the nested UITableViews and delegate any vertical movement to the outer UITableView - and vice-versa.