如何使用CoreGraphics绘制椭圆弧?

Amr*_*emy 5 core-graphics

是否可以在CoreGraphics中绘制类似SVG路径的椭圆弧以及如何?

Joe*_*out 7

今晚我遇到了同样的事情.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);
Run Code Online (Sandbox Code Playgroud)

请注意,如果要绘制饼形楔形,则只需使用CGContextMoveToPoint(cx,cy)和CGContextAddLineToPoint(cx,cy)围绕"CGContextAddPath"调用,并使用CGContextFillPath代替CGContextStrokePath.(或者如果您想同时填充和描边,请使用CGContextDrawPath.)