【问题标题】:iOS rainbow colors arrayiOS 彩虹色数组
【发布时间】:2012-03-11 05:40:34
【问题描述】:

我正在设置一个数组,该数组在彩虹的颜色中具有过渡。现在我刚刚手动输入了数组中的颜色,但是手动输入的颜色太多了......到目前为止,我只是从 0.25 到 0.5 到 0.75 到 1 等等,直到我从红色到绿色再到蓝色然后回来。 (见下面的代码)我怎样才能让数组自动生成不仅仅是 0.25 --> 0.5 --> 0.75 但可能是 0.05 --> 0.10 --> 0.15 --> 0.20 等等的颜色,...这是我的数组:

rainbowColors = [[NSArray alloc] initWithObjects:
                     [UIColor colorWithRed:1 green:0 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.25 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.5 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.75 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.75 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.5 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.25 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.25 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.5 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.75 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.75 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.25 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.25 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.5 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.75 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.75 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.5 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.25 alpha:1],nil];

【问题讨论】:

    标签: objective-c ios arrays rows


    【解决方案1】:

    更简单,使用-[UIColor colorWithHue:saturation:brightness:alpha:],像这样:

    NSMutableArray *colors = [NSMutableArray array];
    
    float INCREMENT = 0.05;
    for (float hue = 0.0; hue < 1.0; hue += INCREMENT) {
        UIColor *color = [UIColor colorWithHue:hue
                                    saturation:1.0
                                    brightness:1.0
                                         alpha:1.0];
        [colors addObject:color];
    }
    

    这允许您改变色调(或颜色)而不改变屏幕上颜色的亮度,您现在很可能不会保留它。它写起来也简单得多,对后来的读者来说也更清楚。

    【讨论】:

    • 但是如果我说 1000 或 10000 个项目要循环通过我如何重置它?在循环中?
    • @ChuckKelly Hue 只能为 0.0-1.0,将所有内容乘以 100,让 for 循环变为 ∞ 循环,始终增加它,并将 colorWithHue 设置为 (float)(hue%100)/ 100.0f
    • 这仍然是我一直以来最喜欢的答案之一哈哈。既然我对编程有了更多的了解,我可以很容易地用 3 个 for 循环回答我自己的问题,甚至 1 个 for 循环,使用一个达到 300% 的变量并使用模数和条件来获得结果,但我从来没有想过只是调整色调。我已经在多个场合使用过这个技巧 :) 即使在游戏中生成图块时,我也只是将饱和度和亮度设置为 1,将色调设置为 arc4ran,非常棒的东西!再次感谢(3 年后)
    【解决方案2】:

    3个嵌套for循环和3个变量r、g、b,每次循环加0.25。

    【讨论】:

      【解决方案3】:

      Swift 5、iOS 13 更新至BJHomer 答案

      extension UIButton {
        func rainbowText() {
          var colors:[UIColor] = []
          let increment:CGFloat = 0.02
          for hue:CGFloat in stride(from: 0.0, to: 1.0, by: increment) {
            let color = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
            colors.append(color)
          }
          var colorIndex = 0
          Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
            if colorIndex < colors.count {
              self.setTitleColor(colors[colorIndex], for: .normal)
              colorIndex = colorIndex + 1
            } else {
              self.setTitleColor(colors[0], for: .normal)
              timer.invalidate()
            }
          }
        }
      }
      

      你这样称呼它......

      buttonOutlet.rainbowText()
      

      【讨论】: