My code:
@IBAction func handlePan(gesture: UIPanGestureRecognizer) {
let transition = gesture.translationInView(self.view)
switch gesture.state{
case .Changed:
if let view = gesture.view {
view.center = CGPoint(x: view.center.x + transition.x, y: view.center.y + transition.y)
}
gesture.setTranslation(CGPointZero, inView: self.view)
default:break
}
}
gesture.setTranslation(CGPointZero, inView: self.view)
As you pan around, the transition
value is from where you first began the gesture.
Look at how you move the button. You just keep adding the larger and larger transition
to each updated center
. What you need to do is add the latest transition
to the center
as it was when the pan gesture was first recognized.
So either reset the translation (like in your posted code), or save the original center
of the button when the gesture's state is .Began
and apply the translation to the original center value.