Why does this CAKeyFrameAnimation stop with zero opacity?

蹲街弑〆低调 提交于 2019-12-08 02:26:55

问题


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];

回答1:


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.



来源:https://stackoverflow.com/questions/1159736/why-does-this-cakeyframeanimation-stop-with-zero-opacity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!