即使我的手指在节点之外,如何让我的节点旋转?

How do I get my node to rotate even if my finger is outside the node?

我会尽力解释这一点。我有这个圆圈,当我的手指在圆圈上移动时,我可以旋转它。我遇到的问题是,当我旋转圆圈并将手指移到圆圈外时,旋转停止。即使我的手指在节点之外,我也希望它仍然有触感。当我在单个视图控制器中但在 spritekit 中我无法弄清楚时,它可以工作。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)


        if node.name == "circle" {

            let dy = circle.position.y - location.y
            let dx = circle.position.x - location.x
            let angle2 = atan2(dy, dx)
            circle.zRotation = angle2


           }
          }
         }

var rotatingNode:SKNode?一样声明一个可选的SKNode来保存当前的旋转节点。您可以覆盖 touchesBegan.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    for touch in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)


        if node.name == "circle" || node.name == "rightCircle" {

            self.rotatingNode = node


           }
          }
         }

从您的评论看来,您在 touchesMoved 中的代码似乎与您问题中发布的代码不同。但是如果你想旋转 circle 或 rightCircle 然后这样说:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {




            let dy = self.rotatingNode?.position.y - location.y
            let dx = self.rotatingNode?.position.x - location.x
            let angle2 = atan2(dy, dx)
            self.rotatingNode?.zRotation = angle2



          }
         }

然后在touchesEnded:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.rotatingNode = nil
    }