mirror of
https://github.com/wnagrodzki/DragGestureRecognizer.git
synced 2025-04-05 03:51:53 +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!
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue