滑动手势给出 NSException

Swipe Gesture gives NSException

我是 swift 编程新手。所以我试图在 iOS 的应用程序中添加滑动手势。但是当我尝试滑动时,它会停止应用程序并说 NSException 被捕获。这是我写的代码。

override func viewDidLoad() {
    super.viewDidLoad()

    textLabels.append(label0)
    textLabels.append(label1)
    textLabels.append(label2)
    textLabels.append(label3)
    textLabels.append(label4)
    textLabels.append(label5)
    textLabels.append(label6)
    textLabels.append(label7)
    textLabels.append(label8)
    textLabels.append(label9)
    textLabels.append(label10)
    textLabels.append(label11)
    textLabels.append(label12)
    textLabels.append(label13)
    textLabels.append(label14)
    textLabels.append(label15)

    setupInitial()
    print(textLabels.count)
    var request = GADRequest()
    request.testDevices = [kGADSimulatorID]
    bannerView.adUnitID = "ca-app-pub-8950283126375215/1694851281"
    bannerView.rootViewController = self
    bannerView.load(request)
    createAndLoadInterstitial()


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

    let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector(("gesture:")))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.view.addGestureRecognizer(swipeDown)
    //setView(view: hideView, hidden: false)

}

func gesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }
    }
}

我也尝试像这样将 UIGestureRecognizerDelegate 添加到我的 viewController class

class ViewController: UIViewController, UIGestureRecognizerDelegate

但是没有用。我还添加了 UIGestureRecognizer。请帮助我,我真的被困在这里了。我正在使用 Xcode 8 和 OSX El Capitan。提前致谢。

字符串类型 Selector() 已被新的 #selector() 弃用。

将您的操作更新为 #selector(gesture(gesture:)),这应该可以解决问题

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(gesture(gesture:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)