2

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

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

1 回答 1

5

a的默认值fillMode(参见此处CAAnimationkCAFillModeRemoved. 您应该将fillMode动画的 设置为kCAFillModeForwards,并将removedOnCompletion属性设置为NO(默认情况下为YES):

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

您会注意到这些属性是在我为您之前的问题编写的源代码中设置的——它们在那里是有原因的。

于 2009-07-22T05:19:08.303 回答