Skip to content

Instantly share code, notes, and snippets.

@yingmu52
Last active January 18, 2018 18:43
Show Gist options
  • Save yingmu52/5c8db4831618c9bac00d8911f781e724 to your computer and use it in GitHub Desktop.
Save yingmu52/5c8db4831618c9bac00d8911f781e724 to your computer and use it in GitHub Desktop.
Code Snippets
func panGestureAction(_ panGesture: UIPanGestureRecognizer) {
let translation = panGesture.translation(in: view)
if panGesture.state == .began {
originalPosition = view.center
originalPositionNC = navigationController?.navigationBar.center
currentPositionTouched = panGesture.location(in: view)
} else if panGesture.state == .changed {
if (translation.y > 0){
view.frame.origin = CGPoint(
x: view.frame.origin.x,
y: view.frame.origin.y + translation.y
)
self.navigationController?.navigationBar.frame.origin = CGPoint(
x: self.view.frame.origin.x,
y: view.frame.origin.y + translation.y
)
panGesture.setTranslation(CGPoint.zero, in: self.view)
}
} else if panGesture.state == .ended {
let velocity = panGesture.velocity(in: view)
if velocity.y >= 150 {
UIView.animate(withDuration: 0.2
, animations: {
self.view.frame.origin = CGPoint(
x: self.view.frame.origin.x,
y: self.view.frame.size.height
)
self.navigationController?.navigationBar.frame.origin = CGPoint(
x: self.view.frame.origin.x,
y: self.view.frame.size.height
)
}, completion: { (isCompleted) in
if isCompleted {
self.dismiss(animated: false, completion: nil)
}
})
} else {
UIView.animate(withDuration: 0.2, animations: {
self.view.center = self.originalPosition!
self.navigationController?.navigationBar.center = self.originalPositionNC
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment