为什么我的for循环没有正确增加strokeColor

时间:2013-05-29 05:05:25

标签: ios for-loop core-graphics

在这个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++;


}

1 个答案:

答案 0 :(得分:2)

bezier路径只有一种笔触颜色。您应该创建路径,设置颜色并在循环的每次迭代中进行描边。看起来您正在将每个同心圆添加到路径中,这意味着将使用您的最终笔触颜色绘制(或透支)所有圆。