Last active
July 6, 2018 23:48
-
-
Save JillevdW/a7cd3e52affdf40a94bf2e696362ee5b to your computer and use it in GitHub Desktop.
No longer naive
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
@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 velocity = gesture.velocity(in: superview!) | |
let decelerationRate = UIScrollView().decelerationRate | |
let projectedPosition = CGPoint( | |
x: frame.origin.x + project(initialVelocity: velocity.x, decelerationRate: decelerationRate), | |
y: frame.origin.y + project(initialVelocity: velocity.y, decelerationRate: decelerationRate) | |
) | |
let nearestCornerPosition = nearestCorner(to: projectedPosition) | |
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