ios - CoreAnimation - 不透明度淡入淡出动画不起作用

标签 ios core-animation avfoundation calayer cakeyframeanimation

我正在尝试创建一个相当简单的 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]; 

最佳答案

我遇到了同样的问题,问题是 - 一层不能包含淡入淡出。 所以你可以像我一样将其他动画添加到父层

CALayer *parentLayer = [CALayer layer];
CALayer *animtingLayer = [CALayer layer];

//FADE IN
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.beginTime = CMTimeGetSeconds(img.startTime);
        animation.duration = CMTimeGetSeconds(_timeline.transitionDuration);
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeBoth;
        animation.additive = NO;
        [parentLayer addAnimation:animation forKey:@"opacityIN"];

//FADE OUT
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.beginTime = CMTimeGetSeconds(CMTimeAdd(img.passTimeRange.start, img.passTimeRange.duration));
        animation.duration = CMTimeGetSeconds(_timeline.transitionDuration);
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeBoth;
        animation.additive = NO;
        [animtingLayer addAnimation:animation forKey:@"opacityOUT"];

    [parentLayer addSublayer:animtingLayer];

关于ios - CoreAnimation - 不透明度淡入淡出动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707104/

相关文章:

iOS UITableView "' 尝试为单元格创建两个动画'”

ios - 无法在 swift 2 中使用 AVCaptureVideoDataOutput 通过 CGDataProviderCopyData 获取像素数据

swift - 为什么我的声音让我的游戏在 Swift Spritekit 中滞后?

ios - iPhone 上的 HTTP 实时流媒体服务器

ios - 为什么我的 BottomText 边框没有禁用,但 topText 可以正常工作?

ios - 调出新 View 非常慢

ios - 动画边界变化时的 CAShapeLayer 路径动画

ios - 什么是高 iOS 内存使用率?

ios - 类型 'Any' 在 xcode 8 Swift 3 中没有下标成员

ios - 实现 iOS Facebook 应用程序照片查看器