BBringardner
BBringardner

Reputation: 87

Why doesn't my for loop increment strokeColor correctly

In this CG tutorial challenge we are asked to increment the color of circles drawn on the view in a for loop. To do so I setup an array with colors and then attempted to implement the colors inside the fore loop with using the incrementing i++ to move to the next object in the index. In the simulator I see all circles with the same color.

How do I set up the for loop to increment the colors to draw one color per circle.

    //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++;


}

Upvotes: 0

Views: 203

Answers (1)

jrturton
jrturton

Reputation: 119242

A bezier path only has one stroke colour. You should be creating the path, setting the colour and stroking within each iteration of the loop. It looks like you're adding each concentric circle to the path, which means that all of the circles will be drawn (or overdrawn) with your final stroke colour.

Upvotes: 2

Related Questions