Skip to content

Instantly share code, notes, and snippets.

@JillevdW
Last active July 6, 2018 09:31
Show Gist options
  • Select an option

  • Save JillevdW/3e78afe0aa001a5adf3136d97fb83a11 to your computer and use it in GitHub Desktop.

Select an option

Save JillevdW/3e78afe0aa001a5adf3136d97fb83a11 to your computer and use it in GitHub Desktop.
Naive dragging
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