I have some code that rotates several SCNNodes around the x axis when the screen tapped like this:
func handleTap(gestureRecognize: UIGestureRecognizer) {
let sceneView = self.view as SCNView
let slice = self.cubes[0...8]
let container = SCNNode()
for node: SCNNode in slice {
container.addChildNode(node)
}
sceneView.scene!.rootNode.addChildNode(container)
container.runAction(SCNAction.rotateByX(CGFloat(M_PI / 2), y: 0.0, z: 0.0, duration: 1), completionHandler: { () -> Void in
println("complete")
})
}
The problem I am facing is that every time this function is called, the nodes, as a rule, reset are in their original position before the action. When the action is finished, they seem to stay in the right place until the screen is repeated. How to make subsequent calls by handleTaprotating them from their current position?
I tried removing nodes from the original parent before adding them to the container, but it has no visible effect.
I also tried using animation
let spin = CABasicAnimation(keyPath: "rotation")
spin.fromValue = NSValue(SCNVector4: SCNVector4(x: -1, y: 0, z: 0, w: 0))
spin.toValue = NSValue(SCNVector4: SCNVector4(x: -1, y: 0, z: 0, w: Float(M_PI_2)))
spin.duration = 3
spin.repeatCount = .infinity
container.addAnimation(spin, forKey: "spin around")
Which had the same effect as the action.
runAction
container.runAction(action, completionHandler: { () -> Void in
println("completed rotation")
for node: SCNNode in slice {
node.removeFromParentNode()
sceneView.scene!.rootNode.addChildNode(node)
}
})
, .