21

我想要一个跨越整个光谱的平滑颜色过渡(即红色、蓝色、绿色、黄色、橙色等)

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

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

4

3 回答 3

56

一种非常简单的方法是使用 UIView 动画块。

[UIView animateWithDuration:1.0 animations:^{
    view.backgroundColor = [UIColor redColor];
}];

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

斯威夫特

UIView.animate(withDuration: 1.0) { 
    view.backgroundColor = .red
}
于 2013-11-05T05:05:33.067 回答
3

在 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");
     }];

不同动画的详细说明请访问:
iOS UIView 教程:如何使用 UIView 动画

于 2016-01-15T11:50:46.530 回答
3

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

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

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

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

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

示例代码:

  1. 做一个局部变量

int _currentColorHue = 0;

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

-(无效)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];
}];
}

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

于 2017-05-02T10:28:22.550 回答