How do you explicitly animate a CALayer's backgroundColor?
Asked Answered
P

2

34

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:

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

Podgorica answered 5/2, 2009 at 22:8 Comment(0)
B
57

You don't need to wrap CGColorRefs 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.

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.

You could save yourself this trouble by using an implicit animation in the first place.

Bridgetbridgetown answered 6/2, 2009 at 1:4 Comment(2)
Sweet. You'd think they'd document the typecast to id somewhere. Yes, I could make my life easier with an implicit animation, but this was bugging me. To make this persist, all you need is to set the animation's fillMode to kCAFillModeForwards and removedOnCompletion to NO.Podgorica
Or you can save yourself loads of time and use the Facebook Pop library: github.com/facebook/pop. Examples here: github.com/maxmyers/FacebookPopEncapsulate
P
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.

Papaverine answered 11/6, 2009 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.