在这个 CG 教程挑战中,我们被要求在 for 循环中增加在视图上绘制的圆圈的颜色。为此,我设置了一个带有颜色的数组,然后尝试在前循环中实现颜色,使用递增的 i++ 移动到索引中的下一个对象。在模拟器中,我看到所有圆圈都具有相同的颜色。
如何设置 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++;
}