我试图从8个选项中生成随机颜色。我发现的所有堆栈溢出帖子/教程都是随机的颜色。在我的prefix.pch我定义8组不同的颜色定义,这是一个简单的例子:如何生成随机UIColor?
#define cola1 209/255.
#define colb1 0/255.
#define colc1 0/255.
#define cold1 1.0/255.
为cola1-8,colb1-8,colc1-8和cold1-8定义不同的颜色值。
然后,我建立了一个随机数发生器:
int randomNumber;
randomNumber = arc4random() %8;
randomNumber = randomNumber + 1;
whatRandomNumberIs = randomNumber;
我又试图建立[UIColor colorWithRed etc]
像这里面的[NSString stringWithFormat:@"cola%i", randomNumber];
:
[UIColor colorWithRed:[NSString stringWithFormat:@"cola%i", whatRandomNumberIs] green:[NSString stringWithFormat:@"colb%i", whatRandomNumberIs] blue:[NSString stringWithFormat:@"colc%i", whatRandomNumberIs] alpha:[NSString stringWithFormat:@"cold%i", whatRandomNumberIs]];
但后来意识到你不能将NSString
放入CGFloat
。
所以现在我卡住了。如何在不使用NSString stringWithFormat的情况下,在红色,绿色,蓝色和alpha值内安装一个从1-8开始的随机数字?是否有另一种方法来返回一个随机的UIColor值,因为我只希望它是特定的颜色?
检查[这](https://www.cocoacontrols.com/controls/monactivityindicatorview),在这里,他是随机生成的颜色...可能会帮助你... –