UIGestureRecognizer - 拖动后分离SKSpriteNode

时间:2017-04-08 14:06:05

标签: ios swift3 uigesturerecognizer skspritenode uipangesturerecognizer

我正在使用UIPanGestureRecognizer用一根手指旋转我的精灵节点(即向上拖动精灵顺时针旋转,逆时针旋转)。

这是我的代码片段,适用于上述功能

        let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        self.view!.addGestureRecognizer(gestureRecognizer)
        handlePan(gestureRecognizer) 

    func handlePan(_ gestureRecognizer:UIPanGestureRecognizer){
        if gestureRecognizer.state == .began {
            var touchLocation = gestureRecognizer.location(in: gestureRecognizer.view)
            touchLocation = self.convertPoint(fromView: touchLocation)
            gestureRecognizer.minimumNumberOfTouches = 1
            self.selectTouchedNode(touchLocation: touchLocation)
        }

        if gestureRecognizer.state == .changed {
            var touchLocation = gestureRecognizer.location(in: gestureRecognizer.view)
            touchLocation = self.convertPoint(fromView: touchLocation)
            var translation = gestureRecognizer.translation(in: gestureRecognizer.view!)
            translation = CGPoint(x: translation.x, y: translation.y)

            self.rotateNode(translation: translation)
        }

        if gestureRecognizer.state == .ended {
            selectedNode.removeAllActions()
        }
    }

    func selectTouchedNode(touchLocation: CGPoint) {
        let touchedNode = self.atPoint(touchLocation)
        if touchedNode is SKSpriteNode{
        selectedNode = touchedNode as! SKSpriteNode
        print ("touched")
        } else {
        selectedNode.removeAllActions()
        }
    }

    func rotateNode(translation: CGPoint) {
        selectedNode.zRotation = translation.y * 0.01
        print ("Rotate")
        print (translation.y)
    }

但是,拖动后我的精灵节点变得“粘滞”,即如果我拖动了sprite1,拖动场景的背景仍然会旋转精灵1直到我拖动sprite2,然后旋转sprite2直到我拖动另一个节点。 / p>

我如何“分离”节点,使其仅在我开始拖动它们的位置时旋转?

0 个答案:

没有答案