滑动手势给NSException

问题描述:

我是swift编程的新手。所以我试图在iOS的应用程序中添加滑动手势。但是,当我尝试刷卡停止应用程序,并说NSException发生。这是我写的代码。滑动手势给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。提前致谢。

+1

什么是例外,它抛出? – Bienemann

+1

@Bienemann感谢您的回复,但我的问题是通过下面的答案解决的。 – Antzion

对于新的#selector(),已弃用了字符型的Selector()

更新您的行动#selector(gesture(gesture:))而且应该修复它

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

非常感谢。这解决了我的问题。 (Y) – Antzion