在UIView中将背景颜色从一种颜色淡化为另一种颜色 [英] Fade background-color from one color to another in a UIView

查看:194
本文介绍了在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:@fadecontext:nil]; 
[UIView setAnimationDuration:1];
[[self view] setBackgroundColor:[UIColor greenColor]];
[UIView commitAnimations];

谢谢StackOverflow,帮助大家学习更多知识。 :)

解决方案

你不需要创建两个UIView和淡入淡出 - 为什么还需要额外多余的视图颜色属性本身是可动画的吗?



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



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


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. :)

解决方案

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

这篇关于在UIView中将背景颜色从一种颜色淡化为另一种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆