Swift – 如何从移动到另一个时从场景中删除滑动手势?
所以我的游戏使用滑动手势,在我didMoveToView()函数我有这些手势初始化:
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场景,我刷卡,它崩溃我的应用程序。 我注意到有人发布了类似的东西,并得到了这个答案
override func willMoveFromView(view: SKView) { for recognizer in self.view.gestureRecognizers! { self.view.removeGestureRecognizer(recognizer) } }
仍然不太清楚在切换到游戏之前如何实现这个和/或从场景中移除手势。 谁能帮忙?
以下内容将删除视图中的所有滑动手势识别器:
override func willMoveFromView(view: SKView) { if view.gestureRecognizers != nil { for gesture in view.gestureRecognizers! { if let recognizer = gesture as? UISwipeGestureRecognizer { view.removeGestureRecognizer(recognizer) } } } }
你正在消除所有的手势,试试这个:
if([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) { [self removeGestureRecognizer:recognizer]; }
希望这可以帮助.. :)
如果这是你想要删除的,用UISwipeGestureRecognizer
replaceUIGestureRecognizer
。
extension SKView { func removeAllGestureRecognizers() { if let objects = gestureRecognizers { for object in objects { if let gestureRecognizer = object as? UIGestureRecognizer { removeGestureRecognizer(gestureRecognizer) } } } } }