在這個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++;
}
我沒有看到你曾經在改變一種新顏色之前撫摸過路徑 - [path stroke]嗎? –
顯示你的setCircleColor方法 –
看來你正在繪製同一個中心的圓圈..?好吧,似乎給出的代碼是好的。圈畫得很好.. ..?你可以顯示你的setCircleColor方法嗎?我認爲中風的方法是正確的..? – Gihan