[英]how to select a random color from a method with label and color objects?
嘗試從這三種定義的顏色屬性運行隨機顏色,
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;
}
編輯:(對於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];
}
這將返回帶有名稱和顏色的字典。
-(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];
}
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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.