iOS彩虹颜色数组

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

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

这样可以改变色调(或颜色),而不必改变屏幕上颜色的亮度,而这些颜色现在很可能不会保留。 写起来也简单得多,而且对于后来的读者来说更加清楚。

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