Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS彩虹颜色阵列_Objective C_Ios_Arrays_Rows - Fatal编程技术网

Objective c iOS彩虹颜色阵列

Objective c iOS彩虹颜色阵列,objective-c,ios,arrays,rows,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:

我正在设置一个数组,它在彩虹的颜色中有一个过渡。现在我刚刚在数组中手动输入颜色,但是太多了,无法手动输入。。。到现在为止,我只是从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个嵌套循环和3个变量r、g、b,每次循环发生时加0.25。

简单得多,使用
-[uicolorWith色调:饱和度:亮度:alpha:][/code>,如下所示:

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];
}
NSMutableArray*colors=[NSMutableArray];
浮动增量=0.05;
对于(浮动色调=0.0;色调<1.0;色调+=增量){
UIColor*color=[UIColor colorWithHue:hue
饱和度:1.0
亮度:1.0
α:1.0];
[颜色添加对象:颜色];
}

这允许您改变色调(或颜色),而无需更改屏幕上的颜色亮度,而您现在很可能无法保留该颜色。它的编写也简单得多,对以后的读者来说也清晰得多。

答案是Swift 5、iOS 13更新


但是,如果我有1000或10000个项目要循环,怎么重置呢?在循环中?@ChuckKelly色调只能为0.0-1.0,将所有值乘以100,使for循环成为一个循环∞ 循环,始终增加,并将colorWithHue设置为(float)(色调%100)/100.0英尺这仍然是我一直以来最喜欢的答案之一哈哈。现在我对编程有了更多的了解,我可以很容易地用3个for循环来回答我自己的问题,甚至1个for循环使用一个300%的变量,并使用模和条件来得到结果,但我从来没有想过只调整色调。我已经在很多场合使用过这个技巧:)即使在游戏中生成瓷砖时,我也只是将饱和度和亮度设置为1,将色调设置为arc4ran,非常棒!再次感谢(3年后)
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()