Iphone 如何为CABASICANIATION设置颜色?
Cabasicanization具有toValue属性,该属性需要id。但Quartz Core不适用于UIColor,它需要CGColor结构。如何为CABASICANIATION提供颜色?只需提供一个Iphone 如何为CABASICANIATION设置颜色?,iphone,ios,ipad,core-animation,cabasicanimation,Iphone,Ios,Ipad,Core Animation,Cabasicanimation,Cabasicanization具有toValue属性,该属性需要id。但Quartz Core不适用于UIColor,它需要CGColor结构。如何为CABASICANIATION提供颜色?只需提供一个CGColor,并将其键入id UIColor *fromColor = [UIColor redColor]; UIColor *toColor = [UIColor yellowColor]; CABasicAnimation *colorAnimation = [CABasicAnimat
CGColor
,并将其键入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;
此示例在1秒内将背景色从红色淡入黄色
另一个例子直接取自:
很抱歉,我有点迂腐,但有点更正。。。UIColor*fromColor=[UIColor redColor];和UIColor*toColor=[UIColor yellowColor];(注意星号)fromValue和toValue支持UIColor,传递CGColor会导致崩溃。@Andy这是在哪里确切记录的?苹果公司自己似乎不知道这一点-检查示例代码“一起设置多个更改的动画”。@Till,实际上你是对的,它不会插值UIColor。@直到我将其用于自定义UIColor属性,当然它崩溃了,我想是因为动画试图将CGColorRef指定给UIColor属性。
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;