SCNNode 方向而不是 eulerAngles

SCNNode orientation instead of eulerAngles

我正在尝试用 UIRotationGestureRecognizer 轮换 SCNNode 并更改 eulerAngles。我在围绕 y 和围绕 z 轴的旋转之间切换。当我只旋转其中一个时,一切都很好。但是当我旋转两者时出现问题。它不再绕轴旋转,而是随机旋转。

我已经阅读了很多答案,尤其是来自 ARGeo 的答案,例如:,并且我了解 eulerAngles 和万向节锁的问题。

但我不明白如何正确使用 orientation 蚂蚁 w 组件。这里有人成功使用 UIRotationGestureRecognizerorientation 而不是 eulerAngles 吗?

下面是如何实施 UIRotationGestureRecognizer 的示例之一。

我从 复制过来所以 post。

private var startingOrientation = GLKQuaternion.identity
private var rotationAxis = GLKVector3Make(0, 0, 0)

@objc private func handleRotation(_ rotation: UIRotationGestureRecognizer) {
    guard let node = sceneView.hitTest(rotation.location(in: sceneView), options: nil).first?.node else {
        return
    }
    if rotation.state == .began {
        startingOrientation = GLKQuaternion(boxNode.orientation)
        let cameraLookingDirection = sceneView.pointOfView!.parentFront
        let cameraLookingDirectionInTargetNodesReference = boxNode.convertVector(cameraLookingDirection,                                                                        
                                                                                 from: sceneView.pointOfView!.parent!)
        rotationAxis = GLKVector3(cameraLookingDirectionInTargetNodesReference)
    } else if rotation.state == .ended {
        startingOrientation = GLKQuaternionIdentity
        rotationAxis = GLKVector3Make(0, 0, 0)
    } else if rotation.state == .changed {
        let quaternion = GLKQuaternion(angle: Float(rotation.rotation), axis: rotationAxis)
        node.orientation = SCNQuaternion((startingOrientation * quaternion).normalized())
    }
}

希望对您有所帮助。