Skip to content

Instantly share code, notes, and snippets.

@mavieth
Created March 31, 2019 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavieth/4a4d9756975df71315fe935d37608295 to your computer and use it in GitHub Desktop.
Save mavieth/4a4d9756975df71315fe935d37608295 to your computer and use it in GitHub Desktop.
Swipe Gesture Recognizer
class PurpleViewController: UIViewController {
fileprivate var label = UILabel()
open override func viewDidLoad() {
super.viewDidLoad()
prepareView()
prepareSwipe()
}
}
extension PurpleViewController {
fileprivate func prepareView() {
isMotionEnabled = true
view.backgroundColor = Color.pink.base
}
// Prepare swipe gestures
fileprivate func prepareSwipe() {
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
view.addGestureRecognizer(swipeLeft)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
view.addGestureRecognizer(swipeUp)
}
// Respond to swipe
@objc
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
dismiss(animated: true) {
print("Dismissed after: \(swipeGesture.direction)")
}
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
dismiss(animated: true) {
print("Dismissed after: \(swipeGesture.direction)")
}
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
dismiss(animated: true) {
print("Dismissed after: \(swipeGesture.direction)")
}
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
dismiss(animated: true) {
print("Dismissed after: \(swipeGesture.direction)")
}
default:
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment