2015-09-27 93 views
1

我試圖用這個動畫改變我的視圖背景顏色,它應該從藍色變爲紅色變成綠色但它不會運行,我無法弄清楚我在做什麼錯誤。ObjC動畫不會運行

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]; 

回答

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];