我想在视频的最后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)
因此,要使它们出现和消失,您需要添加两个动画-每个动画一个。这是对我有用的示例代码。在这种情况下,我在视频的0到1秒之间添加了一个文本层。您应该为动画的开始时间设置自己的值。您应该在开始时将不透明度设置为0。
textLayer.opacity = 0
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")