iOS彩虹色阵

时间:2012-03-11 05:40:34

标签: objective-c ios arrays rows

我正在设置一个数组,它在彩虹的颜色中有一个过渡。现在我只是手动输入数组中的颜色,但手动输入的数量太多了......截至目前我只是从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];

3 个答案:

答案 0 :(得分:41)

更简单,请使用-[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];
}

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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()