gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:42:20 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于iphone - 如何从带有标签和颜色对象的方法中选择随机颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com