在Objective-C中绘制具有起始和结束角度的椭圆

7 objective-c ellipse ios uibezierpath geometric-arc

我正在编写一个iPad应用程序,我在其中渲染表示形状的XML对象到屏幕上的图形.我试图渲染的对象之一是弧.基本上这些弧为我提供了一个边界矩形以及一个开始和结束角度.

给定属性:

  • X
  • ÿ
  • 宽度
  • 高度
  • 由startAngle
  • endAngle

使用这些值,我需要绘制弧(基本上是椭圆的一部分).我不能使用以下内容:

    UIBezierPath *arc = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, width, height)];
    [UIColor blackColor] setStroke];
    [arc stroke];
Run Code Online (Sandbox Code Playgroud)

因为它绘制了一个完整的椭圆.基本上我需要上面但是它需要考虑开始和结束角度,因此只显示椭圆的一部分.我认为这将涉及绘制三次贝塞尔曲线或二次贝塞尔曲线.问题是我不知道如何用我给出的信息计算起点,终点或控制点.

Ken*_*ses 9

您可以通过在椭圆的绘图周围设置剪辑路径来实现您想要的效果.

CGContextSaveGState(theCGContext);
CGPoint center = CGPointMake(x + width / 2.0, y + height / 2.0);
UIBezierPath* clip = [UIBezierPath bezierPathWithArcCenter:center
                                                    radius:max(width, height)
                                                startAngle:startAngle
                                                  endAngle:endAngle
                                                 clockwise:YES];
[clip addLineToPoint:center];
[clip closePath];
[clip addClip];

UIBezierPath *arc = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, width, height)];
[[UIColor blackColor] setStroke];
[arc stroke];

CGContextRestoreGState(theCGContext);
Run Code Online (Sandbox Code Playgroud)

裁剪的确切半径并不重要.它需要足够大,以便它只在末端夹住椭圆,而不是通过所需的弧.