3

ユーザーが SceneView をクリックしてアニメーションを一時停止できるテスト アプリを作成しようとしています。SceneView は、3d アプリ (Cinema 4D) で作成された .dae ファイルからアニメーションを読み込みます。アプリは、起動時にアニメーションを正常に再生およびループします。

アニメーションを一時停止するには、Technical Q&A QA1673を参考にしました。この .dae ファイルの場合、アニメーションは実際にはアニメーションの階層として入ってくるので、基礎となる CAKeyframeAnimation のそれぞれに到達して、その速度をゼロに設定しようとしました。私のコードは現在次のようになっています。

- (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 の速度をゼロに設定しようとしましたが、これも効果がありませんでした。進行中のアニメーションを一時停止する正しい方法は何ですか?

4

2 に答える 2

6

「animationForKey:」によって返されるアニメーションは、実行中のアニメーションのコピーです。ドキュメントには、「返されたオブジェクトのプロパティを変更しようとすると、未定義の動作が発生します」と記載されています。したがって、代わりに次のようなことができます。

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 からのすべてのアニメーションを一時停止したい場合は、次のようにします。

[mySCNView setPlaying:いいえ]; //シーン時間ベースのアニメーションを一時停止

于 2012-08-27T09:45:46.027 に答える
3

pausedまたは、に設定できますtrue

スウィフトの場合:

mySCNView.scene?.paused = true
于 2015-04-17T09:40:19.903 に答える