さまざまな CALayer をアニメーション化しようとしていますが、最後のアニメーションしか機能しません。
テキストを含み、それぞれにアニメーションを追加するさまざまな CALayer を作成するために、これを基本的に行っています。CALayers を生成するコードは次のとおりです。
// Create a layer for the title
CALayer *_watermarkLayer = [CALayer layer];
[_watermarkLayer setOpacity:0];
// Create a layer for the text of the title.
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string = text;
titleLayer.foregroundColor = [color CGColor];
titleLayer.shadowOpacity = 0.5;
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.bounds = CGRectMake(0, 0, videoSize.width/2, videoSize.height/2);
[_watermarkLayer addSublayer:titleLayer];
// Fade in/out animation
NSString* aux = [NSString stringWithFormat:@"%d", seconds];
CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.additive = YES;
fadeAnimation.removedOnCompletion = YES;
fadeAnimation.beginTime = seconds;
fadeAnimation.duration = 2.0;
fadeAnimation.fillMode = kCAFillModeRemoved;
[_watermarkLayer addAnimation:fadeAnimation forKey:[@"animateOpacity" stringByAppendingString:aux]];
上記のコードから取得した CALayer を次のように使用しています。
- (void) addWatermarkWithVideoComposition:(AVMutableVideoComposition*)videoComposition withLabel:(NSString*)text andColor:(UIColor*)color andBeginTimeInSeconds:(int)seconds
{
// Setup video layers
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
videoLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
[parentLayer addSublayer:videoLayer];
// Create and add watermark layer
CALayer *exportWatermarkLayer = [self watermarkLayerForSize:CGSizeMake(300, 300) andText:text andColor:color andBeginTimeInSeconds:seconds];
exportWatermarkLayer.position = CGPointMake(videoComposition.renderSize.width/2, videoComposition.renderSize.height/4);
[parentLayer addSublayer:exportWatermarkLayer];
// Merge layers
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
}
使用法:
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = instructions;
videoComposition.renderSize = outputSize;
videoComposition.frameDuration = CMTimeMake(1, 30);
[self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:0];
[self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:3];
[self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:5];
AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:composition presetName:preset];
....................
私は何を間違っていますか?