user2892270
user2892270

Reputation: 305

How to get the start point of UIPanGesture and constantly calculate the moving distance?

How to get the start point location of UIPanGesture and constantly calculate the moving distance? It would be better if you can provide some Swift code in your answer.

Upvotes: 1

Views: 1184

Answers (2)

user2892270
user2892270

Reputation: 305

I figure it out myself.

let panGesture = UIPanGestureRecognizer(target: self, action: Selector("panFunction:"))

myView.addGestureRecognizer(panGesture)

func panFunction(sender: UIPanGestureRecognizer) {
        let translation = sender.translationInView(self.view)
        let startPoint = CGPointZero
        let distance = Double(translation.y - startPoint.y)
        println("\(distance)")
    }

Upvotes: 1

Leo
Leo

Reputation: 24714

 override func viewDidLoad() {
    super.viewDidLoad()
    let pan = UIPanGestureRecognizer(target: self, action: "catchPaned")
    self.view.addGestureRecognizer(pan)
    self.view.userInteractionEnabled = true
}
func catchPaned(gesture:UIPanGestureRecognizer){
    switch(gesture.state){
    case .Began:
        let touchStart = gesture.locationInView(self.view)
    case .Changed:
        let distance = gesture.translationInView(self.view)
    default:
        println("default")
    }
}

Get start point using locationInView,get distance using translationInView

Upvotes: 2

Related Questions