【问题标题】:Dragging an UIView inside UIScrollView with gesture recognizers enabled for sibling views在 UIScrollView 中拖动 UIView 并为兄弟视图启用手势识别器
【发布时间】:2011-11-14 22:49:32
【问题描述】:

我的应用中有以下视图层次结构:

-- UIScrollView (canCancelContentTouches is NO)
   -- UIView #1 (UISwipeGestureRecognizer is bound to it to track horizontal swipes)
      -- UIView #2 (touchesBegan/touchesMoved/touchesEnded are implemented here to allow
                    dragging this view inside its parent; implementation is very
                    straightforward and I'm NOT calling supermethods here).

当我开始拖动视图 #2 时,它有时会触发滑动手势识别器。我真的看不出这种模式,但这种情况经常发生。

有什么方法可以抑制拖动时的触摸处理吗?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uigesturerecognizer


    【解决方案1】:

    在视图#2 上使用UIPanGestureRecognizer,而不是直接处理触摸。在两个识别器的委托中,从 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 返回 NO。

    UISwipeGestureRecognizer的委托中,实现这个:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        return gestureRecognizer.view == touch.view;
    }
    

    【讨论】:

    • 这肯定行得通,但这些视图并不打算相互了解(即处理完全不同的功能子集)
    • 我在原始问题中的视图层次结构错误,现在已更新。另外,我已经尝试了您的建议并得到了不同的行为。现在,使用您概述的任何选项,视图#2 很难选择:10 次拖动尝试中有 9 次被传递到滑动识别器,而不是用于平移。一旦我能够选择该视图并开始拖动它,就不会再触发滑动(因此最初的问题就消失了)。我可以看到gestureRecognizer:shouldReceiveTouch: 中的touch.view 几乎总是包含#1——即使我正在小心地从#2 的中心拖动。知道为什么吗?
    • 好的,所以在#1 的hitTest: 正确返回#2 后,我解决了这个问题。不得不让#1 依赖于#1。我会接受你的回答,因为它是一个非常有用的指导。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2015-08-16
    • 2014-07-13
    相关资源
    最近更新 更多