iOS - 平滑的颜色变化过渡/动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - Smooth Color Change Transition/Animation
提问by JimmyJammed
I want to have a smooth color transition that goes across the entire spectrum (i.e. red, blue, green, yellow, orange, etc.)
我想要一个跨越整个光谱的平滑颜色过渡(即红色、蓝色、绿色、黄色、橙色等)
Also want to be able to have smooth transitions of colors in specific spectrum (i.e. all reds).
还希望能够在特定光谱(即所有红色)中平滑过渡颜色。
Are there any simple algorithms/recursive functions/formulas that can help simplify this process?
是否有任何简单的算法/递归函数/公式可以帮助简化这个过程?
采纳答案by Lakhpat meena
One possible way of doing it is apply background color animation on view's layer.
一种可能的方法是在视图层上应用背景颜色动画。
Now to pass through entire spectrum you have to work on combinations of three colors.
现在要通过整个光谱,您必须处理三种颜色的组合。
The best way to achieve this is to use 'Hue'.
实现这一目标的最佳方法是使用“色调”。
[[UIColor alloc] initWithHue:135/360.0f saturation:1 brightness:1 alpha:1]
[[UIColor alloc] initWithHue:135/360.0f 饱和度:1 亮度:1 alpha:1]
Now you have to iterate for all Hue values and you will get the smooth transition that goes across all the spectrum.
现在您必须迭代所有色调值,您将获得跨越所有频谱的平滑过渡。
Sample code:
示例代码:
- make a local variable
- 创建一个局部变量
int _currentColorHue = 0;
int _currentColorHue = 0;
- recursive call for change background color.
- 递归调用更改背景颜色。
-(void)animateMyView {
[UIView animateWithDuration:0.01 animations:^{ self.view.layer.backgroundColor = [[UIColor alloc] initWithHue:_currentColorHue/360.0f saturation:1 brightness:1 alpha:1].CGColor; } completion:^(BOOL finished) { _currentColorHue++; if (_currentColorHue > 360) { _currentColorHue = 0; } [self animateMyView]; }]; }
-(void)animateMyView {
[UIView animateWithDuration:0.01 animations:^{ self.view.layer.backgroundColor = [[UIColor alloc] initWithHue:_currentColorHue/360.0f saturation:1 brightness:1 alpha:1].CGColor; } completion:^(BOOL finished) { _currentColorHue++; if (_currentColorHue > 360) { _currentColorHue = 0; } [self animateMyView]; }]; }
You can stop the animation according to your use.
您可以根据使用情况停止动画。
回答by daltonclaybrook
One very simple way to accomplish this would be to use a UIView animation block.
实现此目的的一种非常简单的方法是使用 UIView 动画块。
[UIView animateWithDuration:1.0 animations:^{
view.backgroundColor = [UIColor redColor];
}];
This will interpolate between whatever view
's previous background color was, and red over the course of 1 second.
这将在view
1 秒的过程中在 之前的背景颜色和红色之间进行插值。
Swift:
斯威夫特:
UIView.animate(withDuration: 1.0) {
view.backgroundColor = .red
}
回答by Aamir
Use following code for color transition in iOS, it gives you various configureable options:
1) Delay before starting animation
2) Callback on animation completion
3) Options for animation
在 iOS 中使用以下代码进行颜色转换,它为您提供了各种可配置的选项:
1) 动画开始前的延迟
2) 动画完成时的回调
3) 动画选项
[UIView animateWithDuration:1.0 delay:0.2 options:0 animations:^{
view.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished)
{
NSLog(@"Color transformation Completed");
}];
For Detailed description of different animations please visit:
UIView Tutorial for iOS: How To Use UIView Animation
有关不同动画的详细说明,请访问:
iOS UIView 教程:如何使用 UIView 动画