1

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

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

4 回答 4

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];
}

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

于 2013-03-05T07:33:42.047 回答
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;
}
于 2013-03-05T07:25:52.507 回答
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];

}
于 2015-08-03T15:03:55.510 回答
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];
}

请检查颜色参考 在此处输入图像描述

于 2017-08-01T10:16:05.450 回答