在 UIView 中将背景颜色从一种颜色淡入另一种颜色
如何在 UIView 中从一种背景颜色淡入另一种颜色?
在 javascript (jQuery) 中,可以通过执行以下操作来完成:$('body').animate({ backgroundColor: "#ff00ff"});
如果我将颜色转换为 HSV 会更容易吗?
编辑: 尝试使用 CABasicAnimation 执行此操作,但由于未知原因它闪烁白色。 :/
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
fade.fillMode = kCAFillModeForwards;
fade.removedOnCompletion = NO;
// fade.fromValue = (id)[self.view backgroundColor].CGColor;
fade.toValue = (id)[UIColor greenColor].CGColor;
[self.view.layer addAnimation:fade forKey:@"fade"];
然后我试图找到另一种方法来做到这一点,并偶然发现了“隐式动画”。以下内容只需一秒钟即可完成:
[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:1];
[[self view] setBackgroundColor: [UIColor greenColor]];
[UIView commitAnimations];
感谢 StackOverflow,帮助每个人了解更多信息。 :)
How can I fade from one background-color to another color in a UIView?
In javascript (jQuery) it can be done by doing the following:$('body').animate({ backgroundColor: "#ff00ff"});
Would it be easier if I converted the colors to HSV?
EDIT:
Tried to do it with CABasicAnimation but it flashes white for unknown reasons. :/
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
fade.fillMode = kCAFillModeForwards;
fade.removedOnCompletion = NO;
// fade.fromValue = (id)[self.view backgroundColor].CGColor;
fade.toValue = (id)[UIColor greenColor].CGColor;
[self.view.layer addAnimation:fade forKey:@"fade"];
Then I tried to find another way to do it and stumbled upon "implicit animation". The following works with a nice fade of a second:
[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:1];
[[self view] setBackgroundColor: [UIColor greenColor]];
[UIView commitAnimations];
Thanks, StackOverflow, for helping everyone learn even more. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要创建两个 UIView 和淡入淡出 - 当颜色属性本身可设置动画时,为什么要费心让一个额外的多余视图挂在周围呢?
您可以如上所示使用 CAAnimation,但无需调整两个视图的不透明度,只需调整一个视图的背景颜色即可。
http://developer.apple.com/库/mac/#documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html
You don't need to create two UIView and fade - why bother having an extra superfluous view hanging around when the color attribute itself is animatable?
You can use CAAnimation as shown above, but instead of adjusting the opacity of two views just adjust the backgroundColor of one.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html