2016-11-26 76 views
1

我在屏幕上有三個節點lineBlock,它們是MovableBlock。我想旋轉某人在屏幕上觸摸的lineBlock節點。如何在UIRotationRecognizer(SpriteKit + Swift 3.0)中找到感動的節點

我已經解決了這個在touchedMoved移動正確lineBlock節點:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     touch = touches.first! 
     positionInScene = self.touch?.location(in: self) 
     let previousPosition = self.touch?.previousLocation(in: self) 
     let translation = CGVector(dx: (positionInScene?.x)! - (previousPosition?.x)!, dy: (positionInScene?.y)! - (previousPosition?.y)!) 

     if touchedNode?.name?.contains("LineBlock") == true { 
       (touchedNode as! MovableBlock).selected = true 
       (touchedNode as! MovableBlock).parent!.parent!.run(SKAction.move(by: translation, duration: 0.0)) 
     } 
    } 

但我一直沒能到我的UIRotationRecognizer函數中做同樣的。在我的旋轉功能,它只是旋轉的第一個節點,無論哪個lineBlock(類MovableBlock)我感動:

func rotate(_ sender: UIRotationGestureRecognizer){ 
     if lineBlock.selected == true { 
       lineBlock.run(SKAction.rotate(byAngle: (-(self.rotationRecognizer?.rotation)!*2), duration: 0.0)) 
       rotationRecognizer?.rotation = 0 
     } 
    } 

供參考,在這裏是我如何定義touchedNode(在touchBegan):

touches: Set<UITouch>, with event: UIEvent?) { 
    touch = touches.first! 
    positionInScene = self.touch?.location(in: self) 
    touchedNode = self.atPoint(positionInScene!) 
+0

會喜歡使用touchedNode但touchNode在touchBegan或touchMoved之外變爲零 – nicolime

回答

1

UIGestureRecognizer s有一個location(in:UIView)方法。您可以使用self.view?.convert(sender.location(in: self.view), to: self)獲取UIRotationGestureRecognizer的位置,並使用與touchesBegan中類似的邏輯。

convert(_:to:)將確保該點位於場景的座標空間中。

相關問題