xcode 如何在 Swift 3 中向右滑动以显示新的视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41429241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:41:55  来源:igfitidea点击:

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

iosswiftxcodeuigesturerecognizer

提问by RDowns

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

我希望能够在我的右侧滑动,ViewController这将显示另一个视图控制器CommunitiesViewController.

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

我查看了其他线程并找到了一些这样做的方法,尽管我相信它们适用于 Swift 2。

This is the code I am using in my ViewController:

这是我在我的代码中使用的代码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 CommunitiesViewControllera storyboard ID of CommunitiesID.

我给CommunitiesViewController了一个故事板 ID 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

libc++abi.dylib:以未捕获的 NSException 类型异常终止

回答by Kike Gamboa

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

回答by T. W

wrong selector format, change to:

错误的选择器格式,更改为:

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

or

或者

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