2013-11-24 51 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