SCNAnimationPlayer开始在手势上播放动画

时间:2017-12-17 08:06:23

标签: ios swift scenekit blender

我使用Scenekit / ARKit代码和Blender代替我的3D模型。我有一个带有一些关键帧动画的3D模型 - 在Blender中创建,可以轻松导出到XCode中。没有什么复杂的 - 它只是一只猫向上移动然后向下移动。

Cat Animated

我在tapGestureRecognizer中使用动画控件。一旦我的视图加载,我就将动画设置为暂停状态: -

nonGeometryObjectNode?.childNode(withName: "Armature", recursively: true)?.animationPlayer(forKey: "cat_animated-1")?.paused = true

我的点击手势中的动画控件代码如下所示。

let animationPlayer = hitResults.first?.node.parent?.childNode(withName: "Armature", recursively: true)?.animationPlayer(forKey: "cat_animated-1")
animationPlayer?.animation.autoreverses = true
animationPlayer?.animation.repeatCount = 1
animationPlayer?.animation.duration = 0.8
animationPlayer?.animation.blendInDuration = 0.2
animationPlayer?.animation.blendOutDuration = 0.2
animationPlayer?.paused = true
if (animationPlayer?.paused)! {
    animationPlayer?.play()
}

它工作正常,唯一的问题是它第一次执行点击时只工作一次。我尝试在我的tapGesture代码中使用.paused.stop(),但每次点击时,animationPlayer都不会重播。 SCNAnimationPlayer中没有bool var,或者我可以使用它来检测动画是否已经播放出来,这样我就可以再次使用.stop()然后再使用.play()

我确实看到了两个实例变量animationDidStart和animationDidStop,我认为它可能对管理我需要的东西很有用。但我无法使用这些方法。什么是真正有用的是每当我点击对象时应该用什么来播放和停止/暂停我的动画。任何指针都会有所帮助。

1 个答案:

答案 0 :(得分:1)

我知道这是一个古老的问题,但是如果以后有人偶然发现它,则默认操作是动画完成后将其删除。因此,要再次播放,您需要进行以下设置:

    animationPlayer?.animation.isRemovedOnCompletion = false
相关问题