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);
等等。如果需要,您可以在最后释放路径,或者保留参考以供以后使用。