Created
March 29, 2017 00:02
-
-
Save gyubokbaik/d2ec06fed597759c56aa62c5ff71e9a0 to your computer and use it in GitHub Desktop.
Blog - Dismiss ViewControllers Presented Modally Using Swipe Down
This file contains hidden or 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
// 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