How to set color for CABasicAnimation?

CABasicAnimation has a toValue property that expects an identifier. But Quartz Core does not work with UIColor; instead, it wants a CGColor framework. How can I specify a color for CABasicAnimation?

+5
source share
1 answer

Just point CGColorand bring it to id.

UIColor *fromColor = [UIColor redColor];
UIColor *toColor = [UIColor yellowColor];
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
colorAnimation.duration = 1.0;
colorAnimation.fromValue = (id)fromColor.CGColor;
colorAnimation.toValue = (id)toColor.CGColor;

This example reduces the background color from red to yellow for 1 second.

Another example, taken directly from the Apple code example :

CAKeyframeAnimation* colorAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderColor"];
NSArray* colorValues = [NSArray arrayWithObjects:(id)[UIColor greenColor].CGColor,
            (id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor,  nil];
colorAnim.values = colorValues;
colorAnim.calculationMode = kCAAnimationPaced;
+24
source

All Articles