【问题标题】:Core Animation - animation doesn't work核心动画 - 动画不起作用
【发布时间】:2011-04-09 14:29:03
【问题描述】:

我正在尝试使用此代码为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 的实现......使用纯色有效)。我还在寻找这个问题的答案。

【问题讨论】:

    标签: iphone objective-c core-animation calayer


    【解决方案1】:

    核心动画通过使用插值来工作——计算您指定的键值之间的中间值。如果它是关键帧动画,它会在值数组中的值数 (n) 之间进行插值。如果它是一个基本动画,它会在两个值之间进行插值——你的开始值和结束值。 CA 可以在两种纯色之间进行很好的计算,因为它们只是由 RGBA 浮点值表示,但是它如何在图像之间插入值呢?它必须进行某种我不相信它能够做到的变形。如果您需要变形效果,最好在视图中的背景图像之间进行过渡。默认效果是交叉淡入淡出/渐隐效果。这可能不是您想要的,但它可能会让您非常接近。

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多