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!)
會喜歡使用touchedNode但touchNode在touchBegan或touchMoved之外變爲零 – nicolime