2013-11-05 39 views
18

我想有在整个频谱变(即红,蓝,绿,黄,橙等)iOS版 - 平滑的色彩过渡更改/动画

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

有没有简单的算法/递归函数/公式可以帮助简化这个过程?

回答

41

一个非常简单的方法来完成这将是使用UIView动画块。

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

这将之间进行内插的任何view的先前的背景颜色为,红色在1秒的过程中。

斯威夫特

UIView.animate(withDuration: 1.0) { 
    view.backgroundColor = .red 
} 
+0

感谢您的回答。这很有帮助,但实际上我需要一种算法,因为我必须将颜色代码单独发送到设备。不能只使用UIView的方法... – JimmyJammed

+0

这太棒了。 – VSG24

3

使用以下用于iOS的色彩过渡的代码,它给你的各种configureable选项:
1)延迟启动动画
2)回调的动画完成之前
3 )动画选项

[UIView animateWithDuration:1.0 delay:0.2 options:0 animations:^{ 
     view.backgroundColor = [UIColor greenColor]; 
    } completion:^(BOOL finished) 
    { 
     NSLog(@"Color transformation Completed"); 
    }]; 

对于详细d请访问:
UIView Tutorial for iOS: How To Use UIView Animation

0

这样做的一种可能方式是在视图的图层上应用背景色动画。

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

达到此目的的最佳方法是使用“色相”。

[的UIColor页头] initWithHue:135/360.0f饱和度:1 亮度:1阿尔法: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]; 
}]; 
} 

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