Adds DragGestureRecognizer to draggable view and a method handling the gesture

This commit is contained in:
Wojciech Nagrodzki 2016-09-06 18:46:45 +02:00
parent f64adb11e9
commit 98b9f94619
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569

View file

@ -29,15 +29,35 @@ class ViewController: UIViewController {
@IBOutlet weak var draggableView: UIView! @IBOutlet weak var draggableView: UIView!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. let dragGestureRecognizer = DragGestureRecognizer(target: self, action: #selector(ViewController.handleGesture(gestureRecognizer:)))
draggableView.addGestureRecognizer(dragGestureRecognizer)
} }
override func didReceiveMemoryWarning() { @objc private func handleGesture(gestureRecognizer: DragGestureRecognizer) {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated. switch gestureRecognizer.state {
case .began:
UIView.animate(withDuration: 0.25, animations: {
gestureRecognizer.setTranslation(self.draggableView.center, in: self.view)
self.draggableView.transform = CGAffineTransform(scaleX: 1.15, y: 1.15)
self.draggableView.alpha = 0.5
})
case .changed:
draggableView.center = gestureRecognizer.translation(in: view)
case .cancelled, .failed, .ended:
UIView.animate(withDuration: 0.25, animations: {
self.draggableView.transform = CGAffineTransform.identity
self.draggableView.alpha = 1
})
case .possible:
break
}
} }
} }