【发布时间】:2013-11-15 21:22:33
【问题描述】:
我想要在整个光谱(即红色、蓝色、绿色、黄色、橙色等)上进行平滑的颜色过渡
还希望能够在特定光谱中平滑过渡颜色(即所有红色)。
是否有任何简单的算法/递归函数/公式可以帮助简化此过程?
【问题讨论】:
标签: ios colors transitions spectrum
我想要在整个光谱(即红色、蓝色、绿色、黄色、橙色等)上进行平滑的颜色过渡
还希望能够在特定光谱中平滑过渡颜色(即所有红色)。
是否有任何简单的算法/递归函数/公式可以帮助简化此过程?
【问题讨论】:
标签: ios colors transitions spectrum
实现这一点的一个非常简单的方法是使用 UIView 动画块。
[UIView animateWithDuration:1.0 animations:^{
view.backgroundColor = [UIColor redColor];
}];
这将在 view 之前的背景颜色和 1 秒内的红色之间进行插值。
斯威夫特:
UIView.animate(withDuration: 1.0) {
view.backgroundColor = .red
}
【讨论】:
在 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");
}];
不同动画的详细说明请访问:
UIView Tutorial for iOS: How To Use UIView Animation
【讨论】:
一种可能的方法是在视图层上应用背景颜色动画。
现在要通过整个光谱,您必须处理三种颜色的组合。
实现这一目标的最佳方法是使用“色调”。
[[UIColor alloc] initWithHue:135/360.0f 饱和度:1 亮度:1 alpha:1]
现在您必须迭代所有色相值,您将获得跨越所有光谱的平滑过渡。
示例代码:
int _currentColorHue = 0;
-(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]; }]; }
你可以根据你的使用停止动画。
【讨论】: