From 98b9f946198a384df89bc4bc869503ab0d97979a Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Tue, 6 Sep 2016 18:46:45 +0200 Subject: [PATCH] Adds DragGestureRecognizer to draggable view and a method handling the gesture --- DragGestureRecognizer/ViewController.swift | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/DragGestureRecognizer/ViewController.swift b/DragGestureRecognizer/ViewController.swift index 289f8a1..10fee77 100644 --- a/DragGestureRecognizer/ViewController.swift +++ b/DragGestureRecognizer/ViewController.swift @@ -29,15 +29,35 @@ class ViewController: UIViewController { @IBOutlet weak var draggableView: UIView! override func 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() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. + @objc private func handleGesture(gestureRecognizer: DragGestureRecognizer) { + + 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 + } } - - }