2014-05-19 147 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 –