5

如何在 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,帮助每个人学习更多。:)

4

1 回答 1

1

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

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

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

于 2011-01-24T17:44:29.767 回答