我正在使用平移手势来旋转 SceneKit 场景中的节点。我目前拥有的代码完美地旋转了节点。这是我用来旋转节点的代码:
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
}
}
}
问题是,当您抬起手指并再次开始旋转时,旋转会“重置”。我需要它“保持”旋转,这样当您再次开始平移时,它就会从上次旋转停止的位置继续。
最佳答案
开始吧,经过一些重大更改、其他问题的信息以及不同的方法,我想出了答案。使用此代码,您可以在任何轴(在我的例子中为 y)上完美旋转节点,然后“存储”其旋转以便随后继续使用另一个平移旋转。
var previousRotation = SCNVector4(x: 0, y: 0, z: 0, w: 0)
@objc func pan(gesture: UIPanGestureRecognizer) {
if gesture.numberOfTouches == 2 {
let view = self.view as! SCNView
let node = view.scene!.rootNode.childNode(withName: "Node", recursively: false)
let translate = gesture.translation(in: view)
var newAngle = Float(translate.x) * Float(Double.pi) / 180.0
var rotationVector = SCNVector4()
rotationVector = SCNVector4(x: 0, y: previousRotation.y + Float(translate.y), z: 0, w: previousRotation.w - newAngle)
switch gesture.state {
case .began:
previousRotation = node!.rotation
break
case .changed:
node!.rotation = rotationVector
break
default: break
}
}
}
希望这对将来的其他人有帮助。
关于ios - SceneKit:平移手势不存储先前的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48665037/