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

时间:2016-05-01 17:30:42

标签: ios swift touchesmoved

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

例如我在我的应用程序中有一个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
    }

1 个答案:

答案 0 :(得分:1)

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

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