为什么此CAKeyFrameAnimation以零不透明度停止? [英] Why does this CAKeyFrameAnimation stop with zero opacity?

查看:197
本文介绍了为什么此CAKeyFrameAnimation以零不透明度停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码。在values数组的末尾,您可以看到我为不透明度提供了0.5。但是由于某种原因,当动画停止时,它会再次闪烁,然后使视图完全透明。怎么了

I have this code. At the end of the values array, you can see I provide 0.5 for the opacity. But for some reason, when the animation stops, it flashes once again and then leaves the view completely transparent. What's wrong there?

CALayer *layer = self.layer;
CAKeyframeAnimation *blinkAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
blinkAnim.duration = 1.0;
//blinkAnim.repeatCount = 0;
blinkAnim.autoreverses = NO;

// keyframe times and values
// we want to start fully opaque, fade out, stay faded out and fade back in shortly before the end of the cycle
blinkAnim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0],
                      [NSNumber numberWithFloat:0.4],
                      [NSNumber numberWithFloat:0.6],
                      [NSNumber numberWithFloat:0.85],
                      [NSNumber numberWithFloat:1.0], nil];
blinkAnim.values = [NSArray arrayWithObjects:   [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:1.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.0],
                    [NSNumber numberWithFloat:0.5], nil];
[layer addAnimation:blinkAnim forKey:nil];


推荐答案

默认 fillMode (请参见此处)对于 CAAnimation kCAFillModeRemoved 。您应将动画的 fillMode 设置为 kCAFillModeForwards ,并设置 removedOnCompletion 属性设置为(默认为 YES ):

The default fillMode (see here) for a CAAnimation is kCAFillModeRemoved. You should set the fillMode of your animation to kCAFillModeForwards, and also set the removedOnCompletion property to NO (by default it's YES):

blinkAnim.removedOnCompletion = NO;
blinkAnim.fillMode = kCAFillModeForwards;

您会注意到这些属性是在我为上一个问题写的源代码中设置的-在那里是有原因的。

You'll notice that those properties were set in the source I wrote for your previous question -- they were there for a reason.

这篇关于为什么此CAKeyFrameAnimation以零不透明度停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆