CoreGraphics == 好时光。
已经有一段时间没有任何自由绘制的东西了,现在我所做的就是构建所有绘图操作前面的内容。请记住,从Logo时代开始就有一个隐式的“光标”,您可以移动它而不作为绘图操作,但您必须指定它。我认为一个好的方法(至少对于静态图形)是先创建那些需要绘制的路径,然后将路径重复用于像填充、描边和阴影之类的操作。
CGColorRef fillColor = // yadda
CGColorRef strokeColor = // yadda
const CGFloat radius = 5.0;
// Create the path first - rounded rectangle
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 100.0 - radius, 10.0);
CGPathAddLineToPoint(path, NULL, 10.0 + radius, 10.0);
CGPathAddArcToPoint(path, NULL, 10.0, 10.0, 10.0, 10.0 + radius, radius);
CGPathAddLineToPoint(path, NULL, 10.0, 100.0 - radius);
CGPathAddArcToPoint(path, NULL, 10.0, 100.0, 10.0 + radius, 100.0, radius);
CGPathAddLineToPoint(path, NULL, 100.0 - radius, 100.0);
CGPathAddArcToPoint(path, NULL, 100.0, 100.0, 100.0, 100.0 - radius, radius);
CGPathAddLineToPoint(path, NULL, 100.0, 10.0 + radius);
CGPathAddArcToPoint(path, NULL, 100.0, 10.0, 100.0 - radius, 10.0, radius);
CGPathCloseSubpath(path);
// Then use it in your draw commands
CGContextSetStrokeColor(context, CGColorGetComponents(strokeColor));
CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextSetLineJoin(context, kCGLineJoinMiter);
CGContextSetLineWidth(context, strokeWidth);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathFillStroke);
CGPathRelease(path);
等等,如果你想要,最后可以释放路径,或者保留引用以便以后使用。