简体   繁体   English

核心动画-动画无效

[英]Core Animation - animation doesn't work

I'm attempting to use this code to animate a CALayer 's background color: 我正在尝试使用此代码为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"];

For some reason, this doesn't do anything. 由于某种原因,这没有任何作用。 Simply using: 只需使用:

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

works (although without animation) and using: 工作(尽管没有动画)并使用:

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

works, too (with animation). 也可以(带有动画)。

Why can't I animate the background color? 为什么我不能为背景色设置动画?

EDIT: The problem is related to the fact that my backgroundColor is created with a pattern image (using UIImage 's implementation... Using solid colors works). 编辑:问题与我的backgroundColor是用图案图像创建的(使用UIImage的实现...使用纯色的作品)有关。 I'm still looking for the answer to this problem. 我仍在寻找这个问题的答案。

Core Animation works by using interpolation--calculating intermediate values in between key values you specify. 核心动画通过使用插值进行工作-计算您指定的关键值之间的中间值。 If it's a keyframe animation, it interpolates between the number (n) of values in your values array. 如果是关键帧动画,则会在values数组中的数值(n)之间进行插值。 If it's a basic animation, it interpolates between two values--your start and end values. 如果是基本动画,则会在两个值之间进行插值-您的开始值和结束值。 CA can make the calculation just fine between two solid colors as these are just represented by RGBA float values, but how would it interpolate values between images? CA可以使两种纯色之间的计算很好,因为它们仅由RGBA浮点值表示,但是它将如何在图像之间插值? It would have to do some sort of morphing which I don't believe it is capable of. 它必须进行某种我不相信它能够进行的变形。 If you need a morphing effect, you are probably better off transitioning between background images in a view. 如果需要变形效果,最好在视图中的背景图像之间进行过渡。 The default effect there is a cross fade/dissolve effect. 默认效果是交叉淡入/溶解效果。 That may not be exactly what you're looking for, but it might get you pretty close. 那可能并不完全是您想要的,但可能会使您更加接近。

Best regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM