如果我将手指从我的节点移开并进入 GameView,为什么我的节点会停止旋转?

Why does my node stop rotating if I move my finger off my node and into the GameView?

我有这个节点,如果我的手指在上面,我可以旋转它。我遇到的问题是,当我旋转节点时,我的手指从节点上滑落到视图上,它停止旋转。如果我的手指不在节点上,我怎么还能触摸到?

例如,我的应用程序中有一个 UISlider,如果我将手指放在滑块上并向下移动,然后尝试左右移动滑块,它仍然有效。我如何才能对触摸事件仍然存在的节点执行相同的操作?如果你不明白我在说什么,请告诉我。这是我用来旋转节点的代码:

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"                 
       //lets user rotate when there is one finger on node.
       let dy = circle.position.y - location.y
       let dx = circle.position.x - location.x
       let angle2 = atan2(dy, dx)
       circle.zRotation =  angle2
    }

您应该将最后一个节点存储在实例变量中,然后使用它尝试保持旋转,即使它没有触及它。

class MyClass  {
var lastNodeSelected:SKNode?

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

    if node.name == "circle"                 
       lastNodeSelected = node
    }
}


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

    if lastNodeSelected != nil {
       let touchedNode = lastNodeSelected!                 
       let dy = touchedNode.position.y - location.y
       let dx = touchedNode.position.x - location.x
       let angle2 = atan2(dy, dx)
       touchedNode.zRotation =  angle2
    }

override func touchesEnd(touches: Set<UITouch>, withEvent event: UIEvent?) {
   lastNodeSelected = nil