2017-01-02 1 views
1

私はViewControllerの中で右にスワイプしたいと思っています。これは別のビューコントローラを表示します、CommunitiesViewControllerSwift 3で新しいView Controllerを表示するには右にスワイプする方法は?

私は他のスレッドで見て、私は、彼らがこれは私が私のViewControllerに使用していたコードであるスウィフト2.

のためのものであると考えているものの、これを行うのいくつかの方法を発見した:私が持っている

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 
     } 
    } 
} 

は、 CommunitiesViewControllerのストーリーボードIDはCommunitiesIDです。

しかし、これは動作しないとアプリがクラッシュし、私は次のエラーで右スワイプする場合:

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

答えて

3

間違った選択形式、変更へ:

action: #selector(respondToSwipeGesture) 
func respondToSwipeGesture(gesture: UIGestureRecognizer) 

または

action: #selector(respondToSwipeGesture(_:)) 
func respondToSwipeGesture(_ gesture: UIGestureRecognizer) 
2

試用:

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) 

} 

その後、機能を追加します。

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 
     } 
    } 
} 
関連する問題