I have FrameChangeAnimation class which takes multiple spritesheets and duration. So lets say I have 3 spritesheet each containing 16 blocks of characters then I give ‘3’ seconds as duration. Upon return of FrameChangeAnimation instance as CALayer I attach it to screen and it plays smoothly [16 frames/second]
Now I want to export this animation as video. Problem is that when I attach CABasicAnimation this time with duration. ‘Duration’ parameter doesn’t work and all frames are played in a single second.
GTAnimation *an = [[GTAnimation alloc] initWithFrame:CGRectMake(0, 0, videoSize.width, videoSize.height) withFileName:@"Host_Angry_" withSeconds:3.5];
CALayer *animationLayer = [CALayer layer];
animationLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
[animationLayer addSublayer:an.animLayer];
CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"currentFrame"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:45.0];
fadeAnimation.additive = NO;
fadeAnimation.removedOnCompletion = NO;
fadeAnimation.beginTime = 0.0;
fadeAnimation.duration = 3.5;
fadeAnimation.repeatCount = HUGE_VAL;
fadeAnimation.fillMode = kCAFillModeBoth;
[an.animLayer addAnimation:fadeAnimation forKey:@"currentFrame"];
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
videoLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:animationLayer];
videoLayer.anchorPoint = CGPointMake(0.5, 0.5);
videoLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
animationLayer.anchorPoint = CGPointMake(0.5, 0.5);
animationLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
According to the docs for the AVVideoCompositionCoreAnimationTool class,
“Any animations will be interpreted on the video’s timeline, not real-time, so you should:
Set animations’ beginTime property to 1e-100 rather than 0 (which CoreAnimation replaces with CACurrentMediaTime);