Created
June 9, 2017 19:59
-
-
Save dgyesbreghs/b5c76009ee90525edecbd04858ed7bb0 to your computer and use it in GitHub Desktop.
This file contains 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
// 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