I am using a pan gesture to rotate a node in my SceneKit Scene. The code I currently have rotates the node perfectly. Here is my code I am using to rotate the node:
var previousRotation:Float = 0
@objc func panGestureRecognized(gesture: UIPanGestureRecognizer) {
if gesture.numberOfTouches == 2 {
let view = self.view as! SCNView
let node = view.scene!.rootNode.childNode(withName: "Node", recursively: false)
let translation = gesture.translation(in: view)
var newAngle = Float(translation.x) * Float(Double.pi) / 180.0
newAngle += previousRotation
switch gesture.state {
case .began:
newAngle += previousRotation
break
case .changed:
node!.rotation = SCNVector4(x: 0, y: Float(translation.y), z: 0, w: newAngle)
break
case .ended:
newAngle += previousRotation
break
default: break
}
}
}
The problem is, the rotation "resets" when you lift your fingers and start the rotation again. I need it to "keep" its rotation so when you start panning again it just continues from where the last rotation stopped at.
previousRotation
variable. Shouldn't it bepreviousRotation = newAngle
in the.ended
case? – mnuages