Created
October 17, 2019 10:21
-
-
Save heitara/9c3175154cf49fe591940931d20edcb5 to your computer and use it in GitHub Desktop.
Swift Swipe Guester detector. Detect all four swipe directions. Swift 4
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
| //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