核心动画-动画无效 [英] Core Animation - animation doesn't work

查看:91
本文介绍了核心动画-动画无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码为 CALayer 的背景颜色设置动画:

I'm attempting to use this code to animate a CALayer's background color:

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

也可以(带有动画)。

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

Why can't I animate the background color?

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

The problem is related to the fact that my backgroundColor is created with a pattern image (using UIImage's implementation... Using solid colors works). I'm still looking for the answer to this problem.

推荐答案

Core Animation通过使用插值来计算-计算介于两者之间的中间值您指定的键值。如果是关键帧动画,则会在值数组中的数值(n)个之间插入。如果是基本动画,则会在两个值之间进行插值-您的开始值和结束值。 CA可以使两种纯色之间的计算很好,因为它们仅由RGBA浮点值表示,但是它将如何在图像之间插值?它必须进行某种我不认为能够变形的变形。如果需要变形效果,最好在视图中的背景图像之间进行过渡。默认效果是交叉淡入/溶解效果。那可能并不完全是您想要的东西,但它可能会让您更加接近。

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. 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? 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.

最诚挚的问候。

这篇关于核心动画-动画无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆