如何从带有标签和颜色对象的方法中选择随机颜色?

时间:2013-03-05 07:15:58

标签: iphone ios objective-c uilabel uicolor

尝试从三个定义颜色属性中运行随机颜色

int num1 = rand() %3;
color1= [UIColor colorWithRed:0.5/num1 green:0.7/num1 blue:0.5/num1 alpha:1.0];
[self setColor:color1 setLabel:label1];

int num2 = rand() %3;
color2 = [UIColor colorWithRed:0.5/num2 green:0.7/num2 blue:0.5/num2 alpha:1.0];
[self setColor:color2 setLabel:label2];

int num3 = rand() %3;
color3= [UIColor colorWithRed:0.5/num3 green:0.7/num3 blue:0.5/num3 alpha:1.0];
[self setColor:color3 setLabel:label3];

尝试从颜色对象,标签对象生成随机颜色。

想要为整个颜色变量设置random(),而不仅仅是属性。我怎样才能做到这一点。提前致谢

4 个答案:

答案 0 :(得分:2)

您应该随机更改rgb值....

color1= [self getRandomColor];                
[self setColor:color1 setLabel:firstColorObject];
color2 = [self getRandomColor];;
[self setColor:color2 setLabel:secondColorObject];
color3= [self getRandomColor];
[self setColor:color3 setLabel:thirdColorObject];

方法定义:

-(UIColor *)getRandomColor{
    UIColor *color;
    float randomRed = rand()%3;//3:you can write any number as you wish...
    float randomGreen =rand()%2;//2:you can write any number as you wish...
    float randomBlue =rand()%4;//4:you can write any number as you wish...
    color= [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
    return color;
}

答案 1 :(得分:2)

编辑:(对于ios)

-(NSDictionary *)randomColorAndLabel{
    UIColor *color1= [UIColor colorWithRed:0.2 green:0.4 blue:0.3 alpha:1.0];
    UIColor *color2= [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1.0];
    UIColor *color3= [UIColor colorWithRed:0.4 green:0.4 blue:0.3 alpha:1.0];

    NSDictionary *colorDict1=@{@"color1" : color1};
    NSDictionary *colorDict2=@{@"color2" : color2};
    NSDictionary *colorDict3=@{@"color3" : color3};

    NSArray *colors=@[colorDict1, colorDict2, colorDict3];

    NSInteger randomNumber=arc4random()%3;

    return colors[randomNumber];
}

返回带有颜色和名称的字典,可用于标记。


早先为OSX解决了

我解决了OSX问题,使用NSColor而不是UIColor,方法名称更改为

colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];

-(NSDictionary *)randomColorAndLabel{
    NSColor *color1= [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.3 alpha:1.0];
    NSColor *color2 = [NSColor colorWithCalibratedRed:0.3 green:0.4 blue:0.3 alpha:1.0];
    NSColor *color3= [NSColor colorWithCalibratedRed:0.4 green:0.4 blue:0.3 alpha:1.0];

    NSDictionary *colorDict1=@{@"color1" : color1};
    NSDictionary *colorDict2=@{@"color2" : color2};
    NSDictionary *colorDict3=@{@"color3" : color3};

    NSArray *colors=@[colorDict1, colorDict2, colorDict3];

    NSInteger randomNumber=arc4random()%3;

    return colors[randomNumber];
}

这会返回带有名称和颜色的字典。

答案 2 :(得分:1)

-(UIColor*) randomColor {
    CGFloat r = (float)(arc4random()%256) / 255.f;
    CGFloat g = (float)(arc4random()%256) / 255.f;
    CGFloat b = (float)(arc4random()%256) / 255.f;

    return [UIColor colorWithRed:r green:g blue:b alpha:1.f];

}

答案 3 :(得分:1)

Finlay我找到了随机颜色的解决方案

只需使用以下代码

- (UIColor *)getRandomColor {

    srand48(arc4random());

    float red = 0.0;
    while (red < 0.1 || red > 0.84) {
        red = drand48();
    }

    float green = 0.0;
    while (green < 0.1 || green > 0.84) {
        green = drand48();
    }

    float blue = 0.0;
    while (blue < 0.1 || blue > 0.84) {
        blue = drand48();
    }

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}

请检查颜色参考 enter image description here