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

gam*_*r87 21 core-animation avfoundation calayer cakeyframeanimation ios

我正在尝试创建一个相当简单的CoreAnimation用于AVComposition.我的目标是创建一个CALayer,通过各种子层,淡入淡出标题,然后淡出图像.幻灯片,基本上.这是使用导出到.mov AVAssetWriter.

在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];
Run Code Online (Sandbox Code Playgroud)

问题似乎是'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;
Run Code Online (Sandbox Code Playgroud)

动画中的图像淡入时间相差5秒.就像标题一样,它们的淡出动画效果很好.

任何帮助将不胜感激!

干杯!

编辑

答案都很有帮助,但最终我发现只有一个动画可以添加到a CALayer.淡出动画正在运行,因为它是最后添加的动画.

然后我尝试了CAAnimationGroup,但这不起作用,因为我修改了相同的键值路径.

所以我意识到a CAKeyframeAnimation是最好的.只有我也有一些困难!代码现在正在淡出,但它并没有淡出.我尝试了各种fillMode,改变了持续时间等等.不能让它工作!!

这是我的代码:

    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]; 
Run Code Online (Sandbox Code Playgroud)

com*_*tos 29

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

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];
Run Code Online (Sandbox Code Playgroud)


Mik*_*nov 25

男人,这么多复杂的答案.最简单的方法就是添加自动反转.瞧.

CABasicAnimation *fadeInAndOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAndOut.duration = 5.0;
fadeInAndOut.autoreverses = YES;
fadeInAndOut.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAndOut.toValue = [NSNumber numberWithFloat:1.0];
fadeInAndOut.repeatCount = HUGE_VALF;
fadeInAndOut.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeInAndOut forKey:@"myanimation"];
Run Code Online (Sandbox Code Playgroud)

  • 淡入淡出完成后,此解决方案立即开始淡出.我猜OP希望标题淡入,保持可见一点,然后淡出. (3认同)

Len*_*enK 5

这对我有用:

 let fadeAnimation = CAKeyframeAnimation(keyPath:"opacity")
 fadeAnimation.beginTime = AVCoreAnimationBeginTimeAtZero + start
 fadeAnimation.duration = duration
 fadeAnimation.keyTimes = [0, 1/8.0, 5/8.0, 1]
 fadeAnimation.values = [0.0, 1.0, 1.0, 0.0]
 fadeAnimation.removedOnCompletion = false
 fadeAnimation.fillMode = kCAFillModeForwards
 layer.addAnimation(fadeAnimation, forKey:"animateOpacity")
 layer.opacity = 0.0
Run Code Online (Sandbox Code Playgroud)