繁体   English   中英

ObjC动画无法运行

[英]ObjC animation won't run

我正在尝试使用此动画更改视图背景颜色,它应该从蓝色变为红色到绿色,但无法运行,我无法弄清楚自己在做什么错。

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

您正在尝试将动画添加到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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM