I\'ve got a view and I applied a UIPanGestureRecogniser to this view:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:sel
In Swift 4, use UIGestureRecognizerState.ended.
e.g.
if (gestureRecognizer.state == UIGestureRecognizerState.ended) {
//Move label back to original position (function invoked when gesture stops)
UIView.animate(withDuration: 0.4) {
self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
}
}
Below is all the code you need in a view controller to animate a UILabel with gesture, including when the gesture ends.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var swipeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Create gesture
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(moveLabelBasedOn(gestureRecognizer:)))
//Assign gesture to UILabel
swipeLabel.addGestureRecognizer(gestureRecognizer)
}
//Animate Label in Resopnse to Gesture
@objc func moveLabelBasedOn(gestureRecognizer: UIPanGestureRecognizer) {
let changeInPosition = gestureRecognizer.translation(in: view)
//Move label in response to gesture
swipeLabel.center = CGPoint(x: view.bounds.width / 2 + changeInPosition.x, y: view.bounds.height / 2 + changeInPosition.y)
//Check if gesture ended
if (gestureRecognizer.state == UIGestureRecognizerState.ended) {
//Move label back to original position (function invoked when gesture stops)
UIView.animate(withDuration: 0.4) {
self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
}
}
}
}
Hope this helps.