CAKeyframeAnimation之后的SCNNode位置值不正确

时间:2018-04-24 18:32:48

标签: swift arkit

我在添加到精灵工具包场景(ARKit项目)的SCNNode上启动了CAKeyframeAnimation。 节点完美移动并正确触发动画开始/停止事件。但是,节点的位置值保持不变。

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

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个打印语句都显示相同的值(取决于用例的值):

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

我无法获得有关如何在场景上移动节点位置的任何反馈,因此我无法根据节点的位置执行任何计算。

1 个答案:

答案 0 :(得分:1)

看看您节点的.presentation属性。这是动画在节点上作用时节点的位置。

为您打印fishNode.presentation.position而不是fishNode.position

来自Apple文档:

  

表示节点的属性非常接近   当前显示的节点版本。

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