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

时间:2016-04-13 07:02:50

标签: xcode swift touchesmoved

我试着以最好的方式解释这个问题。我有这个圆圈,当我的手指在圆圈上移动时我可以旋转。我遇到的问题是当我旋转圆圈并且我的手指移动到圆圈外时旋转停止。即使我的手指在节点之外,我仍希望它仍然有触摸。当我在一个视图控制器中,但在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


           }
          }
         }

1 个答案:

答案 0 :(得分:1)

声明一个可选的SKNode来保存当前的旋转节点,如var rotatingNode: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中有其他代码而不是您的问题中发布的代码。但是如果你想要旋转圆圈或右圆圈,那就说:

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
    }