三、CGPath的路径绘制相关方法
//将路径移动到一个点作为起点
void CGPathMoveToPoint(CGMutablePathRef path,const CGAffineTransform * m, CGFloat x, CGFloat y);
//将路径移动到某个点画出一条线
void CGPathAddLineToPoint(CGMutablePathRef path,const CGAffineTransform * m, CGFloat x, CGFloat y);
//向路径中添加一段二次贝塞尔曲线
/*
cpx:控制点的x坐标
cpy:控制点的y坐标
*/
void CGPathAddQuadCurveToPoint(CGMutablePathRef path,const CGAffineTransform * m, CGFloat cpx, CGFloat cpy,CGFloat x, CGFloat y);
//添加一段三次贝塞尔曲线
void CGPathAddCurveToPoint(CGMutablePathRef path,const CGAffineTransform * m, CGFloat cp1x, CGFloat cp1y,CGFloat cp2x, CGFloat cp2y, CGFloat x, CGFloat y);
//这个方法用于闭合路径 调用这个方法后 路径最后的端点将和起点闭合
void CGPathCloseSubpath(CGMutablePathRef path);
//向路径中追加一个矩形
void CGPathAddRect(CGMutablePathRef path, const CGAffineTransform * m, CGRect rect);
//向路径中追加一组矩形
void CGPathAddRects(CGMutablePathRef path, const CGAffineTransform * m, const CGRect * rects,size_t count);
//向路径中追加一组线条
void CGPathAddLines(CGMutablePathRef path, const CGAffineTransform * m, const CGPoint * __nullable points, size_t count);
//添加一组椭圆
void CGPathAddEllipseInRect(CGMutablePathRef cg_nullable path,const CGAffineTransform * m, CGRect rect);
//向路径中追加一组圆弧
/*
x:圆心x坐标
y:圆心y坐标
radius:弧线半径
startAngle:起始角度
endAngle:终止角度
clockwise:是否顺时针绘制
*/
void CGPathAddArc(CGMutablePathRef path, const CGAffineTransform * m, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, bool clockwise);
//向路径中追加一组圆弧
/*
x:圆心x坐标
y:圆心y坐标
radius:弧线半径
startAngle:起始角度
delta:圆弧绘制的长度 为弧度制 2π为整个圆
*/
void CGPathAddRelativeArc(CGMutablePathRef path, const CGAffineTransform * __nullable matrix, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat delta);
//向路径中追加一段圆弧 弧线是以(x1,y1)到(x2,y2)为切线的弧线
void CGPathAddArcToPoint(CGMutablePathRef path,const CGAffineTransform * m, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius);
//向路径中追加一段路径
void CGPathAddPath(CGMutablePathRef path1,const CGAffineTransform * m, CGPathRef path2);
示例代码如下:
- (void)drawRect:(CGRect)rect {
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGPoint center = CGPointMake(rect.size.width/2, rect.size.height/2);
CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, nil, center.x, center.y-50);
CGPathAddLineToPoint(pathRef, nil, center.x+100, center.y);
CGPathAddQuadCurveToPoint(pathRef, nil, 0, 0, center.x+100, center.y-100);
CGPathAddRelativeArc(pathRef, nil, 100, 100, 50, 0, M_PI);
CGPathCloseSubpath(pathRef);
[[UIColor redColor] setStroke];
CGContextAddPath(contextRef, pathRef);
CGContextDrawPath(contextRef, kCGPathStroke);
CGPathRelease(pathRef);
CGContextRelease(contextRef);
}
效果如下图所示:
四、CGPath中的其他方法汇总
//判断某个路径是否为空
bool CGPathIsEmpty(CGPathRef path);
//判断某个路径是否为某个矩形
bool CGPathIsRect(CGPathRef cg_nullable path, CGRect * rect);
//获取某个路径当前绘制所在的点
CGPoint CGPathGetCurrentPoint(CGPathRef path);
//获取某个路径包含所有点的尺寸
CGPathGetBoundingBox(CGPathRef cg_nullable path);
//获取某个路径的尺寸
CGRect CGPathGetPathBoundingBox(CGPathRef path);
//判断路径是否包含某个点
bool CGPathContainsPoint(CGPathRef path, const CGAffineTransform * m, CGPoint point, bool eoFill);
五、关于CGPathElement结构体
当每次向CGPath路径做操作时,操作的过程实际上都会被记录下来,每个操作行为节点都被封装为了CGPathElement结构体,开发者可以通过如下方法来获取所有操作行为:
CGPathApply(pathRef, nil, func);
CGPathApply()方法中的第3个参数为一个函数指针,示例C函数实现如下:
void func(void * __nullable info,
const CGPathElement * element){
printf("%d",(*element).type);
}
CGPathElement结构体的定义如下:
struct CGPathElement {
//操作节点的类型
CGPathElementType type;
//对应的点集
CGPoint * points;
};
//CGPathElementType枚举定义如下
typedef CF_ENUM(int32_t, CGPathElementType) {
//移动到点的操作行为
kCGPathElementMoveToPoint,
//添加线的操作行为
kCGPathElementAddLineToPoint,
//添加二次贝塞尔曲线的操作行为
kCGPathElementAddQuadCurveToPoint,
//添加三次贝塞尔曲线的操作行为
kCGPathElementAddCurveToPoint,
//闭合路径的操作行为
kCGPathElementCloseSubpath
};