2011-04-09 177 views
1

我试图使用此代码动画一个CALayer的背景色:核心动画 - 动画不起作用

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 
animation.duration = 0.3; 
animation.fromValue = (id)[backgroundLayer backgroundColor]; 
animation.toValue = (id)[[UIColor redColor] CGColor]; 
[backgroundLayer addAnimation:animation forKey:@"animateBackgroundColor"]; 

出于某种原因,这并不做任何事情。只需用:

[backgroundLayer setBackgroundColor:[[UIColor redColor] CGColor]]; 

作品(虽然没有动画),并使用:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
animation.duration=0.3; 
animation.fromValue = [NSNumber numberWithFloat:1.0]; 
animation.toValue = [NSNumber numberWithFloat:0.0]; 
[backgroundLayer addAnimation:animation forKey:@"animateOpacity"]; 

作品,太(动画)。

为什么我不能为背景颜色设置动画效果?

编辑:的问题与我的backgroundColor率与图案图像创建(使用UIImage的实现......使用纯色作品)的事实。我仍然在寻找这个问题的答案。

回答

3

核心动画通过使用插值工作 - 计算您指定的键值之间的中间值。如果它是关键帧动画,它会在值数组中的数值(n)之间进行插值。如果它是基本动画,它会在两个值之间进行插值 - 开始和结束值。 CA可以在两种纯色之间进行计算,因为这些只是由RGBA浮点值表示的,但它如何在图像之间插值?它必须做某种形式的变形,我不相信它是有能力的。如果您需要变形效果,您可能更适合在视图中的背景图像之间切换。默认效果是淡入淡出/淡化效果。这可能不是你想要的,但它可能会让你非常接近。

此致敬礼。