gpt4 book ai didi

ios - 为什么我的 for 循环不正确地增加 strokeColor

转载 作者:行者123 更新时间:2023-11-28 22:33:24 31 4
gpt4 key购买 nike

在这个 CG 教程挑战中,我们被要求在 for 循环中增加在 View 上绘制的圆圈的颜色。为此,我设置了一个带有颜色的数组,然后尝试通过使用递增的 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++;


}

最佳答案

贝塞尔曲线路径只有一种描边颜色。您应该在循环的每次迭代中创建路径、设置颜色和描边。看起来您正在将每个同心圆添加到路径中,这意味着所有圆圈都将使用您的最终描边颜色绘制(或 overdraw )。

关于ios - 为什么我的 for 循环不正确地增加 strokeColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16805889/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com