Core graphics 如何使用CoreGraphics绘制椭圆弧?

Core graphics 如何使用CoreGraphics绘制椭圆弧?,core-graphics,Core Graphics,有没有可能在CoreGraphics中绘制一条类似SVG的椭圆弧路径?如何绘制?今晚我遇到了同样的问题。CG不提供绘制非圆弧的简单方法,但可以使用CGPath和合适的变换矩阵来完成。假设您想要一个轴对齐椭圆的弧,从左、上开始,大小为宽度、高度。然后你可以这样做: CGFloat cx = left + width*0.5; CGFloat cy = top + height*0.5; CGFloat r = width*0.5; CGMutablePathRef path = CGPathCr

有没有可能在CoreGraphics中绘制一条类似SVG的椭圆弧路径?如何绘制?

今晚我遇到了同样的问题。CG不提供绘制非圆弧的简单方法,但可以使用CGPath和合适的变换矩阵来完成。假设您想要一个轴对齐椭圆的弧,从左、上开始,大小为宽度、高度。然后你可以这样做:

CGFloat cx = left + width*0.5;
CGFloat cy = top + height*0.5;
CGFloat r = width*0.5;

CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform t = CGAffineTransformMakeTranslation(cx, cy);
t = CGAffineTransformConcat(CGAffineTransformMakeScale(1.0, height/width), t);
CGPathAddArc(path, &t, 0, 0, r, startAngle, endAngle, false);
CGContextAddPath(g->cg, path);

CGContextStrokePath(g);

CFRelease(path);

请注意,如果要绘制饼状楔块,则只需使用CGContextMoveToPoint(cx,cy)和CGContextAddLineToPoint(cx,cy)围绕“CGContextAddPath”调用,并使用CGContextFillPath而不是CGContextStrokePath。(或者如果您想同时填充和笔划,请使用CGContextDrawPath。)

效果很好-注意:不要犯我犯的错误:)。即使使用MoveToPoint,也需要CGAffineTransformMakeTransform转换(因为必须在曲线位于原点时应用变换t,否则Apple的缩放变换也将乘以与原点的偏移!)注意,这也将缩放笔划宽度。