CAKeyframeAnimation 之后的 SCNNode 位置值不正确

Incorrect SCNNode position values after CAKeyframeAnimation

我在添加到 sprite 套件场景(ARKit 项目)的 SCNNode 上启动了 CAKeyframeAnimation。 节点完美移动并正确触发动画 start/stop 事件。但是节点的位置值保持不变。

这是添加的节点,我检查了它只添加了一次:

let plane = SCNPlane(width: 0.1, height: 0.1)
plane.firstMaterial?.diffuse.contents = UIImage(named: "fish_idle_1_\(GameManager.selectedFishSkin+1)")
fishNode = SCNNode(geometry: plane)
fishNode.name = "fish"
fishNode.constraints = [SCNBillboardConstraint()]
sceneView.scene.rootNode.addChildNode(fishNode)

动画启动,工作正常:

    let pendulumSwings = CAKeyframeAnimation(keyPath: "translation")

    pendulumSwings.duration = time
    pendulumSwings.repeatCount = 1
    pendulumSwings.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    pendulumSwings.values = computedVectors
    pendulumSwings.delegate = self

    fishNode.removeAllAnimations()
    fishNode.addAnimation(pendulumSwings, forKey: nil)

委托人代码:

extension Game: CAAnimationDelegate {
func animationDidStart(_ anim: CAAnimation) {
    guard let viewController = viewController else { return }
    guard let first = viewController.anchors.first else { return }
    guard let last = viewController.anchors.last else { return }
    print("START \(fishNode.position)")
    print("SCENE START \(viewController.sceneView.scene.rootNode.childNode(withName: "fish", recursively: false)!.position)")
}

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    guard let viewController = viewController else { return }
    guard let first = viewController.anchors.first else { return }
    guard let last = viewController.anchors.last else { return }
    print("END \(fishNode.position)")
    print("SCENE END \(viewController.sceneView.scene.rootNode.childNode(withName: "fish", recursively: false)!.position)")
}
}

4 个打印语句都显示相同的值(值取决于用例):

开始 SCNVector3(x:0.386469185,y:-0.640618205,z:0.609007716)

我得不到关于节点位置在场景中如何移动的任何反馈,所以我无法根据节点的位置进行任何计算。

查看您节点的 .presentation 属性。这就是动画作用于节点时节点的位置。

对你来说,打印 fishNode.presentation.position 而不是 fishNode.position

来自 Apple 文档:

The presentation node’s properties provide a close approximation to the version of the node that is currently displayed.

https://developer.apple.com/documentation/scenekit/scnnode/1408030-presentation