【发布时间】:2012-01-03 01:50:13
【问题描述】:
我正在尝试创建一个相当简单的 CoreAnimation 以在 AVComposition 中使用。我的目标是创建一个CALayer,它通过各种子层淡入淡出标题,然后淡入淡出图像。基本上是幻灯片。使用AVAssetWriter 将其导出到.mov。
在 WWDC 2011 AVEditDemo 的帮助下,我已经能够获得标题和出现的图像。问题是它们都同时出现在屏幕上!
我创建了不透明度为 0.0 的每一层。然后我添加了一个CABasicAnimation 以将它们从 0.0 淡化到 1.0,使用以下代码:
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeInAnimation.additive = NO;
fadeInAnimation.removedOnCompletion = YES;
fadeInAnimation.beginTime = 1.0;
fadeInAnimation.duration = 1.0;
fadeInAnimation.fillMode = kCAFillModeForwards;
[titleLayer addAnimation:fadeInAnimation forKey:nil];
问题似乎出在“beginTime”属性上。 “1.0”意味着延迟,因此它在动画开始后 1 秒开始。但是,它立即出现在屏幕上。淡出动画
此代码的反面,对于淡出,只是将 fromValue 更改为 1.0 并将 toValue 更改为 0.0。它的开始时间为 4.0 并且运行良好。
我正在使用以下内容创建animatedTitleLayer:
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string =self.album.title;
titleLayer.font = @"Helvetica";
titleLayer.fontSize = videoSize.height / 6;
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6);
titleLayer.foregroundColor = [[UIColor redColor]CGColor];
titleLayer.opacity = 0.0;
动画中的图像淡入淡出的 beginTime 间隔为 5 秒。就像标题一样,它们的淡出动画效果很好。
任何帮助将不胜感激!
干杯!
编辑
答案都很有帮助,但最终我发现只能将一个动画添加到CALayer。淡出动画正在工作,因为它是最后一个添加的。
然后我尝试了一个 CAAnimationGroup,但这不起作用,因为我正在修改相同的键值路径。
所以我意识到CAKeyframeAnimation 是最适合这个的。只有我对此也有一些困难!代码现在可以淡入淡出,但不会淡出。我尝试了各种填充模式,更改了持续时间等。无法使其工作!
这是我的代码:
CAKeyframeAnimation *fadeInAndOut = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
fadeInAndOut.duration = 5.0;
fadeInAndOut.autoreverses = NO;
fadeInAndOut.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:4.0],
[NSNumber numberWithFloat:5.0], nil];
fadeInAndOut.values = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.0], nil];
fadeInAndOut.beginTime = 1.0;
fadeInAndOut.removedOnCompletion = NO;
fadeInAndOut.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeInAndOut forKey:nil];
【问题讨论】:
-
那么您是要提前定位和配置所有图层,还是可以在幻灯片过渡时添加它们?
-
我正在提前定位和配置。感谢这篇文章,我想通了:stackoverflow.com/questions/2184803/…
-
您已经能够将 CALayer 导出为视频?我现在坚持这一点。如果您有任何提示,请回复我的问题。谢谢! stackoverflow.com/questions/10285175/…
-
Keytimes 应该是 0.0-1.0 之间的值,根据 Apple 的文档:developer.apple.com/documentation/quartzcore/…
标签: ios core-animation avfoundation calayer cakeyframeanimation