How can I capture which direction is being panned using UIPanGestureRecognizer?

后端 未结 3 1767
暖寄归人
暖寄归人 2021-01-29 20:43

Ok so I have been looking around at just about every option under the sun for capturing multi-touch gestures, and I have finally come full circle and am back at the UIPanGesture

3条回答
  •  迷失自我
    2021-01-29 21:14

    Here's an easy to detect before the gesture recognizer begins:

    public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let panRecognizer = gestureRecognizer as? UIPanGestureRecognizer else {
            return super.gestureRecognizerShouldBegin(gestureRecognizer)
        }
    
        // Ensure it's a horizontal drag
        let velocity = panRecognizer.velocity(in: self)
        if abs(velocity.y) > abs(velocity.x) {
            return false
        }
        return true
    }
    

    If you want a vertical only drag, you can switch the x and y.

提交回复
热议问题