我想在视频的最后4秒钟内向视频添加文本层。这是我到目前为止所拥有的:
// 1 - Set up the watermark text layer
CATextLayer *waterMarkText = [[CATextLayer alloc] init];
[waterMarkText setFont:@"Helvetica-Neue"];
[waterMarkText setFontSize:30];
[waterMarkText setFrame:CGRectMake(0, 0, self.size.width, 80)];
[waterMarkText setString:@"made with Videofy"];
[waterMarkText setAlignmentMode:kCAAlignmentRight];
[waterMarkText setForegroundColor:[[UIColor whiteColor] CGColor]];
//Fade In the watermark
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 2;
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeInAnimation.beginTime = videoDuration - 4;
fadeInAnimation.removedOnCompletion = NO;
[overlayLayer addAnimation:fadeInAnimation forKey:@"animateOpacity"];
动画已添加到最终视频中,但从视频开始就可以看到水印。如何在动画开始之前将不透明度设置为0?
((我尝试将watermarkText的不透明度设置为0,但这似乎覆盖了动画。)
因此,要使它们出现和消失,您需要添加两个动画,每个动画一个。这是对我有用的示例代码。在这种特殊情况下,我在视频的0到1秒之间添加了一个文本层。您应该为动画的开始时间设置自己的值。
let startVisible = CABasicAnimation(keyPath: "opacity")
startVisible.duration = 0.1 // for appearing in duration
startVisible.repeatCount = 1
startVisible.fromValue = 0.0
startVisible.toValue = 1.0
startVisible.beginTime = 0.0 // overlay time range start second
startVisible.isRemovedOnCompletion = false
startVisible.fillMode = CAMediaTimingFillMode.forwards
textLayer.add(startVisible, forKey: "startAnimation")
let endVisible = CABasicAnimation(keyPath: "opacity")
endVisible.duration = 0.1 // for disappearing in duration
endVisible.repeatCount = 1
endVisible.fromValue = 1.0
endVisible.toValue = 0.0
endVisible.beginTime = 1.0 // overlay time range end second
endVisible.fillMode = CAMediaTimingFillMode.forwards
endVisible.isRemovedOnCompletion = false
textLayer.add(endVisible, forKey: "endAnimation")