【问题标题】:Fade background-color from one color to another in a UIView在 UIView 中将背景颜色从一种颜色淡化为另一种颜色
【发布时间】:2011-01-25 06:46:12
【问题描述】:

如何在 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];

感谢 * 帮助大家学习更多知识。 :)

【问题讨论】:

标签: iphone uiview uicolor caanimation cabasicanimation


【解决方案1】:

您不需要创建两个 UIView 和淡入淡出 - 当颜色属性本身是可动画的时,为什么还要麻烦有一个额外的多余视图?

您可以使用如上所示的 CAAnimation,但无需调整两个视图的不透明度,只需调整一个视图的 backgroundColor。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html

【讨论】:

  • 我想说我有一个 UIView。我会检查你的链接。 :)
最近更新 更多