Swift - 移动到另一个场景时如何从场景中删除滑动手势?
Posted
技术标签:
【中文标题】Swift - 移动到另一个场景时如何从场景中删除滑动手势?【英文标题】:Swift - How to remove swipe gesture from scene when moving to another one? 【发布时间】:2015-01-11 03:05:50 【问题描述】:所以我的游戏使用滑动手势,在我的 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)
在切换到游戏结束之前,仍然不太清楚如何实现这一点和/或从场景中移除手势。有人可以帮忙吗?
【问题讨论】:
【参考方案1】:以下内容会从视图中移除所有滑动手势识别器:
override func willMoveFromView(view: SKView)
if let gestures = view.gestureRecognizers
for gesture in gestures
if let recognizer = gesture as? UISwipeGestureRecognizer
view.removeGestureRecognizer(recognizer)
【讨论】:
它对我不起作用!!!。每次 view.gestureRecognizers 给出 nil。 @PintuRajput 您确定将手势添加到 SKView 吗?当您向视图添加手势时,手势会自动添加到视图的gestureRecognizers
属性(一个数组)中。即使您删除了所有手势,gestureRecognizers
也会引用一个空数组(不是 nil)。【参考方案2】:
你正在移除各种手势,试试这个:
if([recognizer isKindOfClass:[UISwipeGestureRecognizer class]])
[self removeGestureRecognizer:recognizer];
希望这会有所帮助.. :)
【讨论】:
【参考方案3】:如果您想删除 UISwipeGestureRecognizer
,请将 UIGestureRecognizer
替换为 UISwipeGestureRecognizer
。
extension SKView
func removeAllGestureRecognizers()
if let objects = gestureRecognizers
for object in objects
if let gestureRecognizer = object as? UIGestureRecognizer
removeGestureRecognizer(gestureRecognizer)
【讨论】:
以上是关于Swift - 移动到另一个场景时如何从场景中删除滑动手势?的主要内容,如果未能解决你的问题,请参考以下文章
如何在不丢失鼠标抓取的情况下将 QGraphicsItem 移动到另一个场景?