本文介绍了Swift - 当移动到另一个时,如何从场景中删除滑动手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的游戏使用滑动手势,在我的didMoveToView()函数中我初始化了这些手势:

So my game uses swipe gestures, in my didMoveToView() function I have these gestures initialized:

            let swipeRight = UISwipeGestureRecognizer()
            swipeRight.direction = UISwipeGestureRecognizerDirection.Right
            self.view?.addGestureRecognizer(swipeRight)

            let swipeLeft = UISwipeGestureRecognizer()
            swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
            self.view?.addGestureRecognizer(swipeLeft)

            let swipeUp = UISwipeGestureRecognizer()
            swipeUp.direction = UISwipeGestureRecognizerDirection.Up
            self.view?.addGestureRecognizer(swipeUp)

            let swipeDown = UISwipeGestureRecognizer()
            swipeDown.direction = UISwipeGestureRecognizerDirection.Down
            self.view?.addGestureRecognizer(swipeDown)

问题是当我搬到我的GameOver时场景,我刷卡,它崩溃了我的应用程序。我注意到有人发布了类似的东西并将其作为答案

Problem is when I move to my GameOver scene, and I swipe, it crashes my app. I noticed someone had posted something similar and got this as an answer

override func willMoveFromView(view: SKView) {
   for recognizer in self.view.gestureRecognizers! {
       self.view.removeGestureRecognizer(recognizer)
   }
}

仍然不太确定如何实现这一点和/或在切换到游戏之前从场景中删除手势。任何人都可以帮忙吗?

Still not quite sure how to implement this and/or remove the gestures from the scene before switching to game over. Can anyone help?

推荐答案

以下内容从视图中删除所有滑动手势识别器:

The following removes all swipe gesture recognizers from the view:

override func willMoveFromView(view: SKView) {
    if view.gestureRecognizers != nil {
        for gesture in view.gestureRecognizers! {
           if let recognizer = gesture as? UISwipeGestureRecognizer {
                view.removeGestureRecognizer(recognizer)
           }
        }
    }
}

这篇关于Swift - 当移动到另一个时,如何从场景中删除滑动手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:12