如何在CAKeyFrameAnimation中使用关键时间来动画层的不透明度?

时间:2013-03-23 14:37:58

标签: opacity labels cakeyframeanimation

我正在尝试为图层设置动画,以便不透明度在大约0.2秒内从0变为1,将不透明度保持为1秒,然后将不透明度设置为0.我正在尝试使用关键时间要做到这一点,但我做不到。

CAKeyframeAnimation *opacityLabel = [CAKeyframeAnimation animationWithKeyPath: @"opacity"];

// 0.2 seconds fade in, 1 second hold, 0.2 seconds fade out
[opacityLabel setDuration: 1.4];
[opacityLabel setDelegate: self];
[opacityLabel setValue: @"countLabel" forKey: @"verify"];
[opacityLabel setValues: [NSArray arrayWithObjects: [NSNumber numberWithFloat: 1.0], [NSNumber numberWithFloat: 1.0], [NSNumber numberWithFloat: 0], nil]];
[opacityLabel setKeyTimes: [NSArray arrayWithObjects: [NSNumber numberWithFloat: 0.2], [NSNumber numberWithFloat: 1.2], [NSNumber numberWithFloat: 1.4], nil]];

[[tomorrowCountLabel layer] addAnimation: opacityLabel forKey: @"opacityUp"];

1 个答案:

答案 0 :(得分:4)

你快到了。关键时间应该是0到1之间的值。您可以将它们视为百分比。你的动画也应该以不透明度0而不是1开始。所以这里是你应该改变的两行:

opacityLabel.values   = @[@0, @1, @1, @0];
opacityLabel.keyTimes = @[@0, @(0.2/1.4), @(1.2/1.4), @1];