ObjC动画将无法运行

时间:2015-09-27 21:37:08

标签: ios objective-c animation

我试图用这个动画改变我的视图背景颜色,它应该从蓝色变为红色到绿色但它不会运行,我无法弄清楚我做错了什么。

CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:@"backgroundColor" ] ;
anim.values = @[ [UIColor blueColor],[UIColor redColor],[UIColor greenColor] ] ;
anim.autoreverses = NO;
anim.repeatCount = 7.20f ;     
anim.duration = 0.1f ;

UIWindow *win = [[UIApplication sharedApplication]keyWindow];
[win.rootViewController.view.layer addAnimation:anim forKey:nil];

1 个答案:

答案 0 :(得分:0)

您正在尝试向CALayer添加动画。 CALayer的backgroundColor属性的类型为CGColorRef,而不是UIColor。我已按照以下方式修改了您的代码,以使其正常工作:

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
anim.values = @[(__bridge id)[[UIColor blueColor] CGColor],
                (__bridge id)[[UIColor redColor] CGColor],
                (__bridge id)[[UIColor greenColor] CGColor]];
anim.repeatCount = 7.20f ;
anim.duration = 10.0f ;
[self.window.rootViewController.view.layer addAnimation:anim forKey:nil];