5
在CoreGraphics中可以繪製像SVG路徑一樣的橢圓弧嗎?如何用CoreGraphics繪製橢圓弧?
在CoreGraphics中可以繪製像SVG路徑一樣的橢圓弧嗎?如何用CoreGraphics繪製橢圓弧?
今晚我碰到了同樣的事情。 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);
請注意,如果你想畫一個餅形楔,然後就圍繞着「CGContextAddPath」與CGContextMoveToPoint調用(CX,CY)和CGContextAddLineToPoint(CX, cy),並使用CGContextFillPath而不是CGContextStrokePath。 (或者如果你想同時填充和描邊,使用CGContextDrawPath。)
工程很棒 - 注意:不要犯我犯的錯誤:)。即使您使用MoveToPoint,CGAffineTransformMakeTranslation也是必需的(因爲變換t必須在曲線位於原點時應用,否則Apple的比例變換也會將您的偏移量與原點相乘) – Adam 2013-02-18 12:30:45
請注意,縮放筆畫寬度。 – Grodriguez 2017-11-23 09:33:18