I'm trying to construct a CABasicAnimation to animate the backgroundColor property of a Core Animation CALayer, but I can't figure out how to properly wrap a CGColorRef value to pass to the animation. For example:
我正在尝试构建一个CABasicAnimation来动画Core Animation CALayer的backgroundColor属性,但我无法弄清楚如何正确地包装CGColorRef值以传递给动画。例如:
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat values[4] = {1.0, 0.0, 0.0, 1.0};
CGColorRef red = CGColorCreate(rgbColorspace, values);
CGColorSpaceRelease(rgbColorspace);
CABasicAnimation *selectionAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
[selectionAnimation setDuration:0.5f];
[selectionAnimation setToValue:[NSValue valueWithPointer:red]];
[layer addAnimation:selectionAnimation forKey:@"selectionAnimation"];
seems to do nothing to the backgroundColor property, I assume because handing it off as a pointer wrapped in an NSValue is not the way to pass it along.
似乎对backgroundColor属性没有任何作用,我假设因为将它作为包裹在NSValue中的指针移交它不是传递它的方法。
backgroundColor is an animatable property of CALayer, so my question is: how do you set the From or To values for this particular property (preferably in a Mac / iPhone platform-independent way)?
backgroundColor是CALayer的一个可动画属性,所以我的问题是:如何设置此特定属性的From或To值(最好是以Mac / iPhone平台无关的方式)?
2 个解决方案
#1
58
You don't need to wrap CGColorRef
s when setting the toValue
or fromValue
properties of a CABasicAnimation
. Simply use the CGColorRef
. To avoid the compiler warning, you can cast the CGColorRef
to an id
.
设置CABasicAnimation的toValue或fromValue属性时,不需要包装CGColorRefs。只需使用CGColorRef即可。要避免编译器警告,可以将CGColorRef强制转换为id。
In my sample app, the following code animated the background to red.
在我的示例应用程序中,以下代码将背景设置为红色。
CABasicAnimation* selectionAnimation = [CABasicAnimation
animationWithKeyPath:@"backgroundColor"];
selectionAnimation.toValue = (id)[UIColor redColor].CGColor;
[self.view.layer addAnimation:selectionAnimation
forKey:@"selectionAnimation"];
However, when the animation is over, the background returns to the original color. This is because the CABasicAnimation
only effects the presentation layer of the target layer while the animation is running. After the animation finishes, the value set in the model layer returns. So you are going to have to set the layers backgroundColor
property to red as well. Perhaps turn off the implicit animations using a CATransaction
.
但是,当动画结束时,背景将返回原始颜色。这是因为CABasicAnimation仅在动画运行时影响目标图层的表示层。动画结束后,返回模型图层中设置的值。因此,您还必须将图层backgroundColor属性设置为红色。也许使用CATransaction关闭隐式动画。
You could save yourself this trouble by using an implicit animation in the first place.
你可以通过首先使用隐式动画来省去这个麻烦。
#2
2
I was struggling with this problem until I experimentally found the roadblock. Setting the backgroundColor property of the layer, either directly or via the animation, will have no effect if you change the background color from the default (of unspecified) in Interface Builder, in the context of course of an item (such as UILabel) that's been placed in IB. So leave the background color unspecified and the above should work.
在我实验性地发现障碍之前,我一直在努力解决这个问题。直接或通过动画设置图层的backgroundColor属性,如果您在Interface Builder中的默认(未指定)中更改背景颜色,则在项目(例如UILabel)的上下文中将无效被安排在IB。因此,请不要指定背景颜色,以上情况应该有效。
#1
58
You don't need to wrap CGColorRef
s when setting the toValue
or fromValue
properties of a CABasicAnimation
. Simply use the CGColorRef
. To avoid the compiler warning, you can cast the CGColorRef
to an id
.
设置CABasicAnimation的toValue或fromValue属性时,不需要包装CGColorRefs。只需使用CGColorRef即可。要避免编译器警告,可以将CGColorRef强制转换为id。
In my sample app, the following code animated the background to red.
在我的示例应用程序中,以下代码将背景设置为红色。
CABasicAnimation* selectionAnimation = [CABasicAnimation
animationWithKeyPath:@"backgroundColor"];
selectionAnimation.toValue = (id)[UIColor redColor].CGColor;
[self.view.layer addAnimation:selectionAnimation
forKey:@"selectionAnimation"];
However, when the animation is over, the background returns to the original color. This is because the CABasicAnimation
only effects the presentation layer of the target layer while the animation is running. After the animation finishes, the value set in the model layer returns. So you are going to have to set the layers backgroundColor
property to red as well. Perhaps turn off the implicit animations using a CATransaction
.
但是,当动画结束时,背景将返回原始颜色。这是因为CABasicAnimation仅在动画运行时影响目标图层的表示层。动画结束后,返回模型图层中设置的值。因此,您还必须将图层backgroundColor属性设置为红色。也许使用CATransaction关闭隐式动画。
You could save yourself this trouble by using an implicit animation in the first place.
你可以通过首先使用隐式动画来省去这个麻烦。
#2
2
I was struggling with this problem until I experimentally found the roadblock. Setting the backgroundColor property of the layer, either directly or via the animation, will have no effect if you change the background color from the default (of unspecified) in Interface Builder, in the context of course of an item (such as UILabel) that's been placed in IB. So leave the background color unspecified and the above should work.
在我实验性地发现障碍之前,我一直在努力解决这个问题。直接或通过动画设置图层的backgroundColor属性,如果您在Interface Builder中的默认(未指定)中更改背景颜色,则在项目(例如UILabel)的上下文中将无效被安排在IB。因此,请不要指定背景颜色,以上情况应该有效。