可以利用 CGMutablePathRef 创建每个不同图形,然后再一起添加到CGContext中
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
//一、 画线
// 1.先创建一个路径
CGMutablePathRef linePath = CGPathCreateMutable();
// 2.拼接路径
CGPathMoveToPoint(linePath, nil, 0, 0);
CGPathAddLineToPoint(linePath, nil, 100, 100);
// 添加路径到上下文
CGContextAddPath(ctx, linePath);
//二、 画圆
CGMutablePathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect(circlePath, nil, CGRectMake(150, 150, 100, 100));
CGContextAddPath(ctx, circlePath);
// 渲染
CGContextStrokePath(ctx);
// Creat\Copy\retain 用过之后都要做一次release
CGPathRelease(linePath);
CGPathRelease(circlePath);
}