iOS - 平滑变色过渡/动画

Jim*_*med 21 colors transitions spectrum ios

我希望有一个平滑的颜色过渡,遍及整个光谱(即红色,蓝色,绿色,黄色,橙色等)

还希望能够在特定光谱(即所有红色)中平滑过渡颜色.

是否有任何简单的算法/递归函数/公式可以帮助简化此过程?

dal*_*ook 49

实现此目的的一种非常简单的方法是使用UIView动画块.

[UIView animateWithDuration:1.0 animations:^{
    view.backgroundColor = [UIColor redColor];
}];
Run Code Online (Sandbox Code Playgroud)

这将在view之前的背景颜色之间进行插值,并在1秒的过程中进行红色插值.

斯威夫特:

UIView.animate(withDuration: 1.0) { 
    view.backgroundColor = .red
}
Run Code Online (Sandbox Code Playgroud)


小智 4

一种可能的方法是在视图层上应用背景颜色动画。

现在要通过整个光谱,您必须研究三种颜色的组合。

实现这一目标的最佳方法是使用“色调”。

[[UIColor alloc] initWithHue:135/360.0f 饱和度:1 亮度:1 alpha:1]

现在您必须迭代所有色调值,您将获得跨越所有光谱的平滑过渡。

示例代码:

  1. 创建一个局部变量

int _currentColorHue = 0;

  1. 递归调用更改背景颜色。

-(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];
}];
}
Run Code Online (Sandbox Code Playgroud)

您可以根据您的使用情况停止动画。