mirror of
https://github.com/wnagrodzki/DragGestureRecognizer.git
synced 2025-04-05 12:01:58 +02:00
Adds DragGestureRecognizer to draggable view and a method handling the gesture
This commit is contained in:
parent
f64adb11e9
commit
98b9f94619
1 changed files with 26 additions and 6 deletions
|
@ -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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue