滑动手势提供NSException

时间:2017-02-24 18:59:13

标签: ios swift xcode uigesturerecognizer

我是快速编程的新手。所以我试图在我的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。提前谢谢。

1 个答案:

答案 0 :(得分:1)

Selector()已弃用字符串类型#selector()

将您的操作更新为#selector(gesture(gesture:)),并确保修复

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