如何在不抬起手指的情况下触发多个UIGestureRecognizers

时间:2017-02-09 11:57:24

标签: ios swift swift3

我在屏幕上有相邻的视图,每个视图都有不同的UIGestureRecognizer。我想要的是如果我在一个视图中向下触摸并移动到相邻视图中,相邻视图的手势识别器将开始检测触摸。

目前正在发生的事情是,如果我在一个视图中向下触摸并移出该视图的帧,初始视图的手势识别器将继续接收到touchChanged的呼叫,直到我的手指已经解除了。如果我将手势识别器的state更改为.ended.failed.cancelled并不重要,其他任何手势识别器都不会接听他们的电话。 touch方法,直到我的手指被抬起。

如何确保触摸在其视图范围内时触发手势识别器,并在触摸时离开它们,而不必处理来自超级视图的触摸事件?

以下是我实施的自定义UIGestureRecognizer

class GestureRecognizer: UIGestureRecognizer, UIGestureRecognizerDelegate {
    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)
        delegate = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        state = .began
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesMoved(touches, with: event)
        if state == .began {state = .ended}
        else {state = .began}
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return otherGestureRecognizer is GestureRecognizer
    }
}

1 个答案:

答案 0 :(得分:0)

您可以这样做,只需在控制器中编写此委托方法,然后返回true。

阅读一次。 https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer