2013-05-29 99 views
0

在這個CG教程的挑戰中,我們被要求增加for循環中視圖上繪製的圓的顏色。爲此,我使用顏色設置數組,然後嘗試使用遞增的i ++移動到索引中的下一個對象來實現前循環內的顏色。在模擬器中,我看到所有具有相同顏色的圓圈。爲什麼我的for循環不能正確增加strokeColor

如何設置for循環以增加顏色以每個圓圈繪製一種顏色。

//set array of colors up 
NSArray *colors = [[NSArray alloc]initWithObjects:[UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor purpleColor], nil]; 

CGRect bounds = [self bounds]; 

//figure out center point of the bounds rectangle 
CGPoint center; 
center.x = bounds.origin.x + bounds.size.width/2.0; 
center.y = bounds.origin.y + bounds.size.height/2.0; 

//the radius fo the circle should be nearly as big as the view 
float maxRadius = hypot(bounds.size.width, bounds.size.height)/2.0; 

UIBezierPath *path = [[UIBezierPath alloc]init]; 

int i = 0; 

for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { 

    //we have to add this because when drawing several arcs UIBezierPath does not pick up the pencil between arcs and this moves the starting point of the next "stroke" 
    [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)]; 

    [path addArcWithCenter:center radius:currentRadius //note this is the current radius! 
       startAngle:0.0 endAngle:M_PI*2.0 clockwise:YES]; 

     [self setCircleColor:[colors objectAtIndex:i]]; 
    i++; 


} 
+1

我沒有看到你曾經在改變一種新顏色之前撫摸過路徑 - [path stroke]嗎? –

+1

顯示你的setCircleColor方法 –

+0

看來你正在繪製同一個中心的圓圈..?好吧,似乎給出的代碼是好的。圈畫得很好.. ..?你可以顯示你的setCircleColor方法嗎?我認爲中風的方法是正確的..? – Gihan

回答

2

貝塞爾路徑只有一個筆觸顏色。您應該創建路徑,在循環的每次迭代中設置顏色和撫摸。它看起來像是將每個同心圓添加到路徑中,這意味着所有的圓都將使用最終筆劃顏色繪製(或透支)。

+0

感謝移動'UIBezierPath *路徑= [[UIBezierPath alloc] init];'語句到for循環修復了這個問題。 – user2330744

相關問題