【问题标题】:Objective C - Draw multiple arc within circleObjective C - 在圆内绘制多条圆弧
【发布时间】:2023-04-09 14:14:01
【问题描述】:

我正在尝试在圆内绘制圆弧并填充圆弧。这是我正在使用的代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextAddEllipseInRect(ctx, rect);
        CGContextSetFillColor(ctx, CGColorGetComponents([[clrArray objectAtIndex:0] CGColor]));
        CGContextFillPath(ctx);

        CGFloat radius = 12.5;

        CGFloat startAngle1 = DegreesToRadians(0);
        CGFloat endAngle1 = DegreesToRadians(135);

        CGFloat startAngle2 = DegreesToRadians(135);
        CGFloat endAngle2 = DegreesToRadians(270);

        //draw arc
        CGPoint center = CGPointMake(radius,radius);

        //Arc 1
        CGContextAddArc(ctx,
                        center.x,
                        center.y,
                        radius,
                        startAngle1,
                        endAngle1,
                        YES);
        [(UIColor*)[clrArray objectAtIndex:1] set];
        CGContextFillPath(ctx);

        //Arc 2
        CGContextAddArc(ctx,
                        center.x,
                        center.y,
                        radius,
                        startAngle2,
                        endAngle2,
                        YES);
        [(UIColor*)[clrArray objectAtIndex:2] set];
        CGContextFillPath(ctx);

画得不对。如果我只提供一个弧线,那么如果它的角度小于 180 度,那么它也不是大胆的。我做错了什么?

编辑:这是正在发生的事情的图像

我在这张图片中使用了起始角度 0 和结束角度 45

【问题讨论】:

  • 什么意义上是“不正确”?
  • 正在画但画错了。就像我给的天使 0 将是 x=0 y=center.y 和 135 将是圆的角度。但它却在右下角绘制它。
  • 如果我给出结束角度,它会准确地绘制在圆的一半末端和起点将与我提到的相同

标签: ios objective-c core-graphics


【解决方案1】:

得到另一个使用贝塞尔路径的解决方案

CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGFloat radius = 50.0/2.0;
        CGPoint center = CGPointMake(radius,radius);

        CGFloat startAngle1 = DegreesToRadians(0);
        CGFloat endAngle1 = DegreesToRadians(120);

        CGFloat startAngle2 = DegreesToRadians(120);
        CGFloat endAngle2 = DegreesToRadians(240);

        CGContextSaveGState(ctx);

        UIBezierPath* clip1 = [UIBezierPath bezierPathWithArcCenter:center
                                                            radius:radius
                                                        startAngle:startAngle1
                                                          endAngle:endAngle1
                                                         clockwise:YES];
        [clip1 addLineToPoint:center];
        [clip1 closePath];
        [clip1 addClip];

        UIBezierPath *arc1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)];
        [[UIColor blackColor] set];
        [arc1 fill];

        CGContextRestoreGState(ctx);

        CGContextSaveGState(ctx);

        UIBezierPath* clip2 = [UIBezierPath bezierPathWithArcCenter:center
                                                             radius:radius
                                                         startAngle:startAngle2
                                                           endAngle:endAngle2
                                                          clockwise:YES];
        [clip2 addLineToPoint:center];
        [clip2 closePath];
        [clip2 addClip];

        UIBezierPath *arc2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)];
        [[UIColor orangeColor] set];
        [arc2 fill];

        CGContextRestoreGState(ctx);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多