2014-05-19 148 views
2

我試圖從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值,因爲我只希望它是特定的顏色?

+0

檢查[這](https://www.cocoacontrols.com/controls/monactivityindicatorview),在這裏,他是隨機生成的顏色...可能會幫助你... –

回答

2

以下是你可以做什麼......

prefix.pch你有如下。

#define colorCombination1 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination2 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination3 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination4 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination5 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination6 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination7 [UIColor colorWithRed:.... alpha:1.0]; 
#define colorCombination8 [UIColor colorWithRed:.... alpha:1.0]; 

現在你創造的這個顏色陣列..

NSArray *myColorArray = [[NSArray alloc] initWithObjects:colorCombination1, colorCombination2, colorCombination3, colorCombination4, colorCombination5, colorCombination6, colorCombination7, colorCombination8, nil]; 

現在你得到的隨機數說變量generatedRandomNumber

UIColor *myRandomColor = [myColorArray objectAtIndex:generatedRandomNumber%8]; 

generatedRandomNumber%8會給你從generatedRandomNumber的餘額。

希望這是你想要的。

2

,您就可以得到隨機的顏色是通過使用huesaturationbrightness

//random color 
CGFloat hue = (arc4random() % 256/256.0); // 0.0 to 1.0 
CGFloat saturation = (arc4random() % 128/256.0) + 0.5; // 0.5 to 1.0, away from white 
CGFloat brightness = (arc4random() % 128/256.0) + 0.5; // 0.5 to 1.0, away from black 
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; 
+0

我看到該教程,但我不明白我可以如何使它在8個值之間,因爲這是任何隨機顏色 –

+0

@LiamStockhomme:你已經定義了8種顏色?然後放在數組中,並使用randomNumber%8作爲數組索引... –

+0

@FahimParkar它沒有工作,因爲它不讓我使它成爲UIColor –

0

你可以創建的UIColor類別和包裹預定義的顏色的方法,類似於這樣:

@interface UIColor (myCategory) 
+ (UIColor *)randomColorForInt(int n); 
@end 

@implementation 
+ (UIColor *)randomColorForInt(int n) { 
    if (n == 0) { 
     return [UIColor colorWithRed:cola1 green:colb1 blue:colc1 alpha:cold1]]; 
    } 
    ... 
} 
@end 
1

您正試圖在運行時構造一個字符串,然後將其用作在編譯時定義的宏的名稱。這是行不通的。在運行時沒有關於編譯時宏名稱的信息。

以下是從編譯時定義的集合中選擇隨機顏色的一種正確方法。定義一個方法返回一個隨機顏色,在一個類別上UIColor

@interface UIColor (Liam_RandomColor) 

+ (UIColor *)Liam_randomColor; 

@end 

實現該方法以第一(僅一次)初始化所述預定顏色的陣列,和第二(每次)返回一個元件該陣列隨意的:

@implementation UIColor (Liam_RandomColor) 

+ (UIColor *)Liam_randomColor { 
    static dispatch_once_t onceToken; 
    static NSArray *colors; 
    dispatch_once(&onceToken, ^{ 
     colors = @[ 
      [UIColor colorWithRed:209/255.0 green:0 blue:0 alpha:1/255.0], 
      [UIColor colorWithRed:50/255.0 green:100/255.0 blue:100/255.0 alpha:1], 
      // etc. 
     ]; 
    }); 

    return colors[arc4random_uniform(colors.count)]; 
} 

@end 
+0

對不起,如果我聽起來很無知,但將其插入UIColor並調用它的最佳方法是什麼? –

+0

閱讀[「用Objective-C編程*」中的「分類添加方法到現有類」](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/DOC/UID/TP40011210-CH6-SW2)。 –

+0

謝謝@rob maynoff –