我正在设置一个数组,在彩虹的颜色中有一个过渡.现在我只是手动输入数组中的颜色,但是有太多的手动键入…到目前为止,我从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];
解决方法
@H_403_7@ 更简单,使用 – [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]; }
这可以让您改变色调(或颜色),而不会改变屏幕上颜色的亮度,现在您很有可能无法保存.写得更简单,对于后来的读者来说也更加清晰.