2016-11-26 2 views
1

Ich habe drei Knoten lineBlock der gleichen Klasse MovableBlock auf dem Bildschirm. Ich möchte den lineBlock Knoten, den jemand auf dem Bildschirm berührt, drehen.So finden Sie den berührten Knoten in einem UIRotationRecognizer (SpriteKit + Swift 3.0)

Ich habe löse dies die richtigen lineBlock Knoten in touchedMoved zum Bewegen:

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)) 
     } 
    } 

Aber ich habe nicht in der Lage, das gleiche in meiner UIRotationRecognizer Funktion zu tun. In meiner Drehfunktion, es dreht sich nur um den ersten Knoten, unabhängig davon, welche lineBlock (der Klasse MovableBlock) Ich bin berührend:

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

Als Referenz hier ist, wie ich touchedNode (in touchBegan) definieren:

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

würde gerne touchedNode verwenden, aber touchedNode wird außerhalb von touchBegan oder touchMoved null – nicolime

Antwort

1

UIGestureRecognizer s haben eine location(in:UIView) Methode. Sie können verwenden, um die Position UIRotationGestureRecognizer abzurufen und eine ähnliche Logik wie in touchesBegan zu verwenden.

convert(_:to:) wird sicherstellen, dass der Punkt im Koordinatenraum Ihrer Szene ist.

Verwandte Themen