How to draw an elliptical arc with CoreGraphics?

Is it possible to draw an elliptical arc such as an SVG path in CoreGraphics and how?

+5
source share
1 answer

Today I came across the same thing. CG does not offer an easy way to draw a non-circular arc, but you can do it with CGPath and a suitable transformation matrix. Suppose you want the arc of an ellipse aligned along the axis to start on the left, top, and with width and height. Then you can do something like this:

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);

, -, CGContextAddPath CGContextMoveToPoint (cx, cy) CGContextAddLineToPoint (cx, cy) CGContextFillPath CGContextStrokePath. (, , CGContextDrawPath.)

+5

All Articles