我对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 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)

关于ios - 滑动手势使NSException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42446324/

10-10 17:37