暂停SceneKit动画 [英] Pausing a SceneKit animation

查看:166
本文介绍了暂停SceneKit动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个测试应用,用户可以在其中通过单击SceneView暂停动画. SceneView从在3d应用程序(Cinema 4D)中创建的.dae文件加载动画.该应用在启动后成功播放并循环播放动画.

I'm trying to create a test app in which the user can pause an animation by clicking in the SceneView. The SceneView loads the animation from a .dae file created in a 3d app (Cinema 4D). The app successfully plays and loops the animation upon launch.

要暂停动画,我使用了技术问题与解答QA1673 作为参考.在此.dae文件的情况下,动画实际上是作为动画层次结构出现的,因此我尝试深入到每个基础CAKeyframeAnimation并将其速度设置为零.我的代码当前如下所示:

To pause the animation, I used Technical Q&A QA1673 as a reference. In the case of this .dae file, the animation actually comes in as a hierarchy of animations, so I have tried reaching down to each underlying CAKeyframeAnimation and setting its speed to zero. My code currently looks like this:

- (void)mouseDown:(NSEvent *)event {

     SCNNode *cubeNode = [self.scene.rootNode childNodeWithName:@"C4D_Cube" recursively:YES];
     CAAnimation *cubeAnimation = [cubeNode animationForKey:@"Cube_Anim_01-02-1"];      
     CAAnimationGroup *cubeAnimationGroup = (CAAnimationGroup *)cubeAnimation;

     // cubeAnimationGroup contains 3 CAAnimationGroups, each of which contains a CAKeyframeAnimation.
     // So I directly access each CAKeyframeAnimation and set its speed to zero.
     for (CAAnimationGroup *subGroup in [cubeAnimationGroup animations]) {
          CFTimeInterval pausedTime = CACurrentMediaTime();
          [[subGroup animations] setValue:@0.0 forKey:@"speed"];
          [[subGroup animations] setValue:[NSNumber numberWithFloat:pausedTime] forKey:@"timeOffset"];
     }
}

设置断点时,可以看到关键帧动画的速度确实从1变为0,但是在场景视图中动画继续以其正常速度播放.我最初尝试仅将顶级CAAnimationGroup上的速度设置为零,但这也没有任何效果.暂停进行中的动画的正确方法是什么?

When I set a breakpoint, I can see that the speed of the keyframe animations does change from 1 to 0, but the animation continues to play at its normal speed in the scene view. I originally tried just setting the speed on the top level CAAnimationGroup to zero, but this also had no effect. What's the correct way to pause an animation in progress?

推荐答案

"animationForKey:"返回的动画是正在运行的动画的副本. 该文档说:尝试修改返回对象的任何属性将导致未定义的行为." 因此,您可以改为执行以下操作:

The animations returned by "animationForKey:" are copies of the running animations. The documentation says "Attempting to modify any properties of the returned object will result in undefined behavior." So you could do something like this instead:

for(NSString *key in [myNode animationKeys]){
    CAAnimation *animation = [myNode animationForKey:key];
    [animation setSpeed:0]; //freeze
    [animation setTimeOffset:CACurrentMediaTime() - [animation beginTime]]; //move back in time
    [cube addAnimation:animation forKey:key]; //re-add the animation with the same key to replace
}

请注意,如果您只想暂停来自.DAE的所有动画,则可能需要执行以下操作:

Note that if you just want to pause all the animations coming from a .DAE you might want to do:

[mySCNView setPlaying:NO];//暂停基于场景时间的动画

[mySCNView setPlaying:NO]; //pause scene-time based animations

这篇关于暂停SceneKit动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆