Last active
July 6, 2018 09:31
-
-
Save JillevdW/3e78afe0aa001a5adf3136d97fb83a11 to your computer and use it in GitHub Desktop.
Naive dragging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func setupGestureRecognizer() { | |
| let gesture = UIPanGestureRecognizer(target: self, action: #selector(wasDragged(gesture:))) | |
| addGestureRecognizer(gesture) | |
| isUserInteractionEnabled = true | |
| } | |
| @objc func wasDragged(gesture: UIPanGestureRecognizer) { | |
| if gesture.state == .began || gesture.state == .changed { | |
| let translation = gesture.translation(in: superview!) | |
| guard let currentPosition = currentSnapPosition else { return } | |
| frame.origin = CGPoint(x: currentPosition.x + translation.x, y: currentPosition.y + translation.y) | |
| } | |
| if gesture.state == .ended { | |
| let nearestCornerPosition = nearestCorner(to: frame.origin) | |
| UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 100, initialSpringVelocity: 30, options: [.curveEaseOut, .allowUserInteraction], animations: { | |
| self.frame.origin = nearestCornerPosition | |
| }, completion: nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment