macos 你如何明确地为 CALayer 的 backgroundColor 设置动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/518192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:02:11  来源:igfitidea点击:

How do you explicitly animate a CALayer's backgroundColor?

iphonecocoamacoscore-animation

提问by Brad Larson

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 平台无关的方式)?

回答by Jason Medeiros

You don't need to wrap CGColorRefs when setting the toValueor fromValueproperties of a CABasicAnimation. Simply use the CGColorRef. To avoid the compiler warning, you can cast the CGColorRefto an id.

CGColorRef设置 a 的toValuefromValue属性时不需要包装s CABasicAnimation。只需使用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 CABasicAnimationonly 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 backgroundColorproperty 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.

您可以首先使用隐式动画来避免这个麻烦。

回答by Phil Oliver

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。所以不指定背景颜色,上面的应该可以工作。