Skip to content

Instantly share code, notes, and snippets.

@dgyesbreghs
Created June 9, 2017 19:59
Show Gist options
  • Save dgyesbreghs/b5c76009ee90525edecbd04858ed7bb0 to your computer and use it in GitHub Desktop.
Save dgyesbreghs/b5c76009ee90525edecbd04858ed7bb0 to your computer and use it in GitHub Desktop.
// MARK: - Actions
extension ViewController {
func swipeView(_ gesture: UIPanGestureRecognizer) {
guard let subView = gesture.view else { return }
guard let beginCenter = beginCenter else {
if gesture.state == .began {
self.beginCenter = subView.center
}
return
}
let translation = gesture.translation(in: swipeView)
let centerX = (view.frame.size.width / 2)
let centerY = (subView.center.y + translation.y)
let minY = (beginCenter.y)
let maxY = (beginCenter.y + swipeViewHeight)
if centerY >= minY && centerX <= maxY {
subView.center = CGPoint(x: centerX,
y: centerY)
}
if gesture.state == .ended {
let finalPoint: CGPoint
if centerY < (beginCenter.y + (swipeViewHeight / 2)) {
isHidden = false
bottomConstraint?.constant = 0
finalPoint = beginCenter
} else {
isHidden = true
bottomConstraint?.constant = -swipeViewHeight
finalPoint = CGPoint(x: beginCenter.x, y: beginCenter.y + swipeViewHeight)
}
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
subView.center = finalPoint
}, completion: nil)
}
gesture.setTranslation(CGPoint(x: 0, y: 0), in: swipeView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment