2013-11-24 10 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.0과 1.0 사이의 임의의 값에 arc4random_uniform를 호출하고 있었다 무엇

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; 

. 당신이를 만들 필요가 정확히 무엇이다 - 당신은 1과 0 사이에 이미 임의의 값에 arc4_random_uniform()를 호출하는 것처럼

관련 문제