2013-11-24 54 views
0

所以我有一个标签,我希望能够按下我已设置的按钮并更改标签的RGB值。看起来很简单,但我很难过。有任何想法吗?随机化标签的颜色

NSInteger r = arc4random()%255; 
NSInteger g = arc4random()%255; 
NSInteger b = arc4random()%255; 

_label.textColor= [UIColor colorWithRed:(arc4random_uniform(r/255.0)) green:(arc4random_uniform(g/255.0)) blue:(arc4random_uniform(b/255.0)) alpha:1] ; 
+0

什么是'(arc4random_uniform(r/255.0))'部分应该做的?不是'r'已经是随机的吗? – Undo

+0

另外,什么具体不工作? – Undo

+0

标签颜色不会按预期更改。我知道按钮连接正确,因为我已经用[UIColor greenColor] – foo

回答

1

您需要

[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] ; 

更换

[UIColor colorWithRed:(arc4random_uniform(r/255.0)) green:(arc4random_uniform(g/255.0)) blue:(arc4random_uniform(b/255.0)) alpha:1] ; 

由于您目前正在这已经在1和0之间的随机值调用arc4_random_uniform() - 这是你需要创建一个什么颜色。

1

你的问题是不必要的双重使用随机。试试这个:

NSInteger r = arc4random_uniform(255); 
NSInteger g = arc4random_uniform(255); 
NSInteger b = arc4random_uniform(255); 

UIColor *color = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]; 
_label.textColor = color; 

你曾经在0.0和1.0之间的随机值上调用arc4random_uniform