iOS-平滑的颜色变化过渡/动画 [英] iOS - Smooth Color Change Transition/Animation

查看:109
本文介绍了iOS-平滑的颜色变化过渡/动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在整个光谱范围内(即红色,蓝色,绿色,黄色,橙色等)具有平滑的颜色过渡

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?

推荐答案

一种可行的方法是在视图的图层上应用背景色动画.

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饱和度:1亮度:1 alpha:1]

[[UIColor alloc] initWithHue:135/360.0f saturation:1 brightness:1 alpha:1]

现在,您必须迭代所有Hue值,您将获得遍及所有光谱的平滑过渡.

Now you have to iterate for all Hue values and you will get the smooth transition that goes across all the spectrum.

示例代码:

  1. 创建局部变量

int _currentColorHue = 0;

int _currentColorHue = 0;

  1. 递归更改背景颜色.

-(void)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.

这篇关于iOS-平滑的颜色变化过渡/动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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