gpt4 book ai didi

uigesturerecognizer - UIGestureRecognizer 如何与 UIResponder 链一起工作?

转载 作者:行者123 更新时间:2023-12-04 23:39:55 25 4
gpt4 key购买 nike

我知道触摸事件可以传递给点击 View 或手势。但在我的演示中有些让我感到困惑:

我的 Root View 中有两个 subview ,一个是 testView(UIView 的子类),另一个是 testBtn(UIButton),它的操作是“testBtnClicked”。然后,我将 UITapGestureRecognizer 添加到我的 Root View 并将其操作设置为“tapAction”。

问题来了:

当我触摸 Root View 时,它会触发“touchsBegan:withEvent:”、“tapAction”和“touchsCancelled:withEvent”方法。这就是我所期望的;

当我触摸 testView 时,它的工作原理与上面相同;

当我触摸testBtn时,它只触发了“testBtnClicked”方法。为什么?

为什么当我触摸 testView 而不是 Root View 时手势被识别?为什么当我触摸 testBtn 时它没有被识别?

最佳答案

啊哈!也许我得到了答案:

首先,gestureRecognizers 获得了识别触摸的第一个机会,正如 App Doc 所说:

A window delays the delivery of touch objects to the view so that the gesture recognizer can analyze the touch first. During the delay, if the gesture recognizer recognizes a touch gesture, then the window never delivers the touch object to the view, and also cancels any touch objects it previously sent to the view that were part of that recognized sequence.



其次,在 iOS 6.0 及更高版本中,UIButton 操作可防止重叠手势识别器行为,正如 Apple Doc 所说:

In iOS 6.0 and later, default control actions prevent overlapping gesture recognizer behavior. For example, the default action for a button is a single tap. If you have a single tap gesture recognizer attached to a button’s parent view, and the user taps the button, then the button’s action method receives the touch event instead of the gesture recognizer. This applies only to gesture recognition that overlaps the default action for a control, which includes:


  • 单指单击 UIButton、UISwitch、UIStepper,
    UISegmentedControl 和 UIPageControl。
  • 单指在 UISlider 的旋钮上滑动,方向与 slider 平行。
  • UISwitch 旋钮上的单指平移手势,方向与开关平行。

  • If you have a custom subclass of one of these controls and you want to change the default action, attach a gesture recognizer directly to the control instead of to the parent view. Then, the gesture recognizer receives the touch event first. As always, be sure to read the iOS Human Interface Guidelines to ensure that your app offers an intuitive user experience, especially when overriding the default behavior of a standard control.

    关于uigesturerecognizer - UIGestureRecognizer 如何与 UIResponder 链一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41098032/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com