Skip to content

Instantly share code, notes, and snippets.

@JillevdW
Last active July 6, 2018 23:48
Show Gist options
  • Save JillevdW/a7cd3e52affdf40a94bf2e696362ee5b to your computer and use it in GitHub Desktop.
Save JillevdW/a7cd3e52affdf40a94bf2e696362ee5b to your computer and use it in GitHub Desktop.
No longer naive
@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