Swift:从左到右设置Pan Gesture

ale*_*ove 4 gesture ios swift

我想设置一个Pan Gesture并将其指定给一个视图,使其根据手指的动态动态动画仅向右和向左移动.我不知道怎么做因为我开始了,非常感谢!

小智 22

在swift中检查PanGesture方向可能看起来像这样.

extension UIPanGestureRecognizer {

    func isLeft(theViewYouArePassing: UIView) -> Bool {
        let detectionLimit: CGFloat = 50
        var velocity : CGPoint = velocityInView(theViewYouArePassing)
        if velocity.x > detectionLimit {
            print("Gesture went right") 
            return false
        } else if velocity.x < -detectionLimit {
            print("Gesture went left")
            return true
        }
    }
}

// Then you would call it the way you call extensions
var panGesture : UIPanGestureRecognizer

// and pass the view you are doing the gesture on
panGesture.isLeft(view) // returns true or false
Run Code Online (Sandbox Code Playgroud)

你也可以创建一个没有扩展的方法,但我认为这种方式非常好.

希望这有助于任何人试图找到答案!