我想在视频中添加带动画的不同图像。 我有这个用户AVMutableAudioCompostion。 我在玩两张图片的过程中取得了成功。两幅图像正常动画。 但是,当我尝试动画三个以上的图像时,我面临着最终视频的问题。 最终视频动画只有最后两张图片。 我已粘贴代码段。
NSArray *imgArray = @[@"Logo3.png",@"Logo2.png",@"Logo1.png",@"Logo4.png",@"Logo5.png"];
NSMutableArray *layers = [NSMutableArray new];
CGFloat timeOffset = 0.0f;
for (NSString *imgName in imgArray) {
UIImage *animationImage = [UIImage imageNamed:imgName];
CALayer *overlayLayer = [CALayer layer];
[overlayLayer setContents:(id)[animationImage CGImage]];
overlayLayer.frame = CGRectMake(10, size.height/2 - 200, 103, 79);
[overlayLayer setMasksToBounds:YES];
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"hidden"];
animation.repeatCount=HUGE_VALF;
// animation.autoreverses=YES;
// animation.cumulative = YES;
// animate from invisible to fully visible
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:0.0];
// animation.fillMode = kCAFillModeForwards;
animation.beginTime = AVCoreAnimationBeginTimeAtZero + timeOffset;
animation.duration = .5f;
animation.removedOnCompletion = YES;
[overlayLayer addAnimation:animation forKey:@"animateOpacity"];
[layers addObject:overlayLayer];
timeOffset += 0.5f;
}
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, size.width, size.height);
videoLayer.frame = CGRectMake(0, 0, size.width, size.height);
[parentLayer addSublayer:videoLayer];
for (CALayer *layer in layers){
[parentLayer addSublayer:layer];
}
composition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
答案 0 :(得分:0)
我怀疑你的问题就在这一行:
[overlayLayer addAnimation:animation forKey:@"animateOpacity"];
这里的“forKey”值实际上是动画名称的任意用户定义字符串。如果使用相同的密钥,则会覆盖以前的动画。尝试每次循环更改forKey名称。
CABasicAnimation的keyPath属性实际上是告诉动画要更改哪个属性。