How do you explicitly animate a CALayer's backgroundColor?


How do you explicitly animate a CALayer's backgroundColor?



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


Displaying a list of views

1:

CFReadStreamHasBytesAvailable polling - best practices
You don't need to wrap CGColorRefs when setting the toValue or fromValue properties of a CABasicAnimation. Objective-C / Cocoa: What is the “Application” proxy object in Interface Builder / Nib good for?Simply use the CGColorRef. Get Return Value of NSTimer's TargetTo avoid the compiler warning, you must cast the CGColorRef to an id.. The file an IconRef comes from In my sample app, the following code animated the background to red.. Should I save strings returned by NSLocalizedString()?
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. Traversing multiple Core Data objects using NSPredicateThis is for the reason this the CABasicAnimation only effects the presentation layer of the tarreceive layer while the animation is running. How do you draw text in a custom view using Objective-C?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..

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


94 out of 100 based on 59 user ratings 254 reviews

*