【问题标题】:UIGesture Recognizer Crashes ApplicationUIGesture 识别器崩溃应用程序
【发布时间】:2014-11-23 18:30:21
【问题描述】:

我试图使用以下方法在 Swift 中向我的 SKScene 添加 UI 手势识别器

let recognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
        recognizer.direction = UISwipeGestureRecognizerDirection.Right
        self.view?.addGestureRecognizer(recognizer)

        func handleSwipe() {
            println("Swiped Right")

但是,一旦我运行并执行滑动,应用程序就会崩溃并显示以下日志:

2014-11-22 23:21:35.251 Revolution[83866:460316] -[Revolution.levelSelect handleSwipe:]:无法识别的选择器发送到实例 0x7feec842b920 2014-11-22 23:21:35.255 Revolution[83866:460316] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Revolution.levelSelect handleSwipe:]:无法识别的选择器发送到实例 0x7feec842b920”

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

有什么我需要添加到 GameViewController 或者选择器导致它崩溃的原因吗?

【问题讨论】:

    标签: swift sprite-kit uigesturerecognizer


    【解决方案1】:

    “handleSwipe:”中的“:”表示方法handleSwipe 接受一个参数。在这种情况下,手势对象被传递给手势处理程序。所以,你的功能应该是

    func handleSwipe(gesture:UISwipeGestureRecognizer) {
        println("Swiped Right")
    }
    

    【讨论】:

    • 它仍然无法运行。我感觉它与 GameViewController 有关。 UIGesturerecognizer 应该在那里初始化吗?
    • 不,你应该在 GameScene.m 的didMoveToView 中初始化它。 handleSwipe() 是否也在 didMoveToView 中声明?从你的帖子我看不出来。
    • 是的。 let recognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") recognizer.direction = UISwipeGestureRecognizerDirection.Right self.view?.addGestureRecognizer(recognizer) func handleSwipe(gesture: UISwipeGestureRecognizer) { println("Swiped Right") }
    • 你需要将handleSwipe移出didMoveToView方法。
    猜你喜欢
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2014-01-28
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多