Skip to content

Instantly share code, notes, and snippets.

@gyubokbaik
Created March 29, 2017 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyubokbaik/d2ec06fed597759c56aa62c5ff71e9a0 to your computer and use it in GitHub Desktop.
Save gyubokbaik/d2ec06fed597759c56aa62c5ff71e9a0 to your computer and use it in GitHub Desktop.
Blog - Dismiss ViewControllers Presented Modally Using Swipe Down
// define a variable to store initial touch position
var initialTouchPoint: CGPoint = CGPoint(x: 0,y: 0)
@IBAction func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
let touchPoint = sender.location(in: self.view?.window)
if sender.state == UIGestureRecognizerState.began {
initialTouchPoint = touchPoint
} else if sender.state == UIGestureRecognizerState.changed {
if touchPoint.y - initialTouchPoint.y > 0 {
self.view.frame = CGRect(x: 0, y: touchPoint.y - initialTouchPoint.y, width: self.view.frame.size.width, height: self.view.frame.size.height)
}
} else if sender.state == UIGestureRecognizerState.ended || sender.state == UIGestureRecognizerState.cancelled {
if touchPoint.y - initialTouchPoint.y > 100 {
self.dismiss(animated: true, completion: nil)
} else {
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment