【问题标题】:how to select a random color from a method with label and color objects?如何从带有标签和颜色对象的方法中选择随机颜色?
【发布时间】:2013-03-05 07:15:57
【问题描述】:

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

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() 用于整个颜色变量,而不仅仅是属性。我怎样才能做到这一点。提前致谢

【问题讨论】:

标签: iphone ios objective-c uilabel uicolor


【解决方案1】:

您应该随机更改 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;
}

【讨论】:

  • 当我设置 random()%3; 时,我得到 9 种不同的颜色,但我只需要 3 种颜色沿着 3 个标签随机移动
【解决方案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];
}

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

【讨论】:

  • 我如何声明 NSColor 标识符,这表示不是一个有效的声明。谢谢
  • @user2038249:正如我所说,它是为 osx 编码的。阅读答案的前两行。将NSColor 更改为UIColor 并将colorWithCalibratedRed: 更改为colorWithRed:
  • 嗨 Anoop:为什么我收到这个错误 NSDictionary *colorDict2=@{@"color2" : color2};Expected ';'after top level delator'
  • NSDictionary *colorDict2=@{@"color2" : color2};错误应为';'在顶级声明符之后'
  • NSDictionary *colorDict1=@{@"color1" : color1}; 呢?评论下两行并检查。
【解决方案3】:
-(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];

}

【讨论】:

    【解决方案4】:

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

    请检查颜色参考

    【讨论】:

      猜你喜欢
      • 2014-12-26
      • 1970-01-01
      • 2015-06-19
      • 2014-10-07
      • 2019-10-29
      • 2017-03-26
      • 2019-03-29
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多