Swift - How to remove swipe gesture from scene when moving to another one?

前端 未结 3 1014
死守一世寂寞
死守一世寂寞 2021-01-03 14:32

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

            let swipeRight = UISwipeGestureRecognizer()
            


        
相关标签:
3条回答
  • 2021-01-03 15:02

    You are removing all kind of gesture, try this:

    if([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
        [self removeGestureRecognizer:recognizer];
    }
    

    Hope this helps.. :)

    0 讨论(0)
  • 2021-01-03 15:02

    Replace UIGestureRecognizer with UISwipeGestureRecognizer if that's what you want to remove.

    extension SKView {
        func removeAllGestureRecognizers() {
            if let objects = gestureRecognizers {
                for object in objects {
                    if let gestureRecognizer = object as? UIGestureRecognizer {
                        removeGestureRecognizer(gestureRecognizer)
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 15:08

    The following removes all swipe gesture recognizers from the view:

    override func willMoveFromView(view: SKView) {
        if let gestures = view.gestureRecognizers {
            for gesture in gestures {
               if let recognizer = gesture as? UISwipeGestureRecognizer {
                    view.removeGestureRecognizer(recognizer)
               }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题