Swift: adjust horizontal movement from left to right

I would like to set the panorama gesture and assign it to the view so that it moves only to the right and left in accordance with the movement of the finger with dynamic animation. I do not know how to do this because I am starting, thank you very much!

+4
source share
1 answer

Checking the direction of PanGesture in swift might look something like this.

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

You can also create such a method without extensions, but I think this method is really good.

Hope this helps anyone trying to find the answer to this question.

+22
source

All Articles