Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swipe right to show new View Controller in Swift 3?

I want to be able to swipe right in my ViewController which will show another view controller, CommunitiesViewController.

I have looked on other threads and found some ways of doing this though I believe they are for Swift 2.

This is the code I am using in my ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("respondToSwipeGesture")))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)
}

  func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    print ("Swiped right")

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.right:


            //change view controllers

            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

            let resultViewController = storyBoard.instantiateViewController(withIdentifier: "CommunitiesID") as! CommunitiesViewController

            self.present(resultViewController, animated:true, completion:nil)    


        default:
            break
        }
    }
}

I have given CommunitiesViewController a storyboard ID of CommunitiesID.

But this does not work and the app crashes when I swipe right with the following error:

libc++abi.dylib: terminating with uncaught exception of type NSException

like image 623
RDowns Avatar asked Dec 14 '22 01:12

RDowns


1 Answers

Try with:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Gesture Recognizer     
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right

    self.view.addGestureRecognizer(swipeRight)
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeLeft)

}

then add the function:

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            //right view controller
            let newViewController = firstViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        case UISwipeGestureRecognizerDirection.left:
            //left view controller
            let newViewController = secondViewController() 
            self.navigationController?.pushViewController(newViewController, animated: true)
        default:
            break
        }
    }
}
like image 63
Kike Gamboa Avatar answered Feb 22 '23 20:02

Kike Gamboa