【问题标题】:iOS - Smooth Color Change Transition/AnimationiOS - 平滑的颜色变化过渡/动画
【发布时间】:2013-11-15 21:22:33
【问题描述】:

我想要在整个光谱(即红色、蓝色、绿色、黄色、橙色等)上进行平滑的颜色过渡

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

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

【问题讨论】:

    标签: ios colors transitions spectrum


    【解决方案1】:

    实现这一点的一个非常简单的方法是使用 UIView 动画块。

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

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

    斯威夫特

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

    【讨论】:

    • 感谢您的回答。这很有帮助,但我实际上需要一个算法,因为我必须将颜色代码单独发送到设备。不能只使用 UIView 的方法...
    • 这太棒了。
    【解决方案2】:

    在 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

    【讨论】:

      【解决方案3】:

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

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

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

      [[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];
      }];
      }
      

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

      【讨论】:

        猜你喜欢
        • 2011-05-05
        • 2013-11-19
        • 2014-02-11
        • 2022-12-25
        • 2014-03-17
        • 1970-01-01
        • 2015-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多