我試圖從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),在這裏,他是隨機生成的顏色...可能會幫助你... –