Skip to content

Instantly share code, notes, and snippets.

@heitara
Created October 17, 2019 10:21
Show Gist options
  • Select an option

  • Save heitara/9c3175154cf49fe591940931d20edcb5 to your computer and use it in GitHub Desktop.

Select an option

Save heitara/9c3175154cf49fe591940931d20edcb5 to your computer and use it in GitHub Desktop.
Swift Swipe Guester detector. Detect all four swipe directions. Swift 4
//more info on https://stackoverflow.com/questions/24215117/how-to-recognize-swipe-in-all-4-directions
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
}
// Example Tabbar 5 pages
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 5 {
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment