//常用属性
/*
1.CGPath: 将UIBezierPath类转换成CGPath
2.currentPoint: 当前path的位置,可以理解为path的终点
3.lineWidth: 线条宽度
4.lineCapStyle: 端点样式
5.lineJoinStyle: 连接类型
6.flatness: 绘线的精细程度,默认为0.6,数值越大,需要处理的时间越长
7.usesEvenOddFillRule: 判断奇偶数组的规则绘制图像,图形复杂时填充颜色的一种规则。类似棋盘
8.miterLimit: 最大斜接长度(只有在使用kCGLineJoinMiter是才有效,最大限制为10), 边角的角度越小,斜接长度就会越大,为了避免斜接长度过长,使用lineLimit属性限制,如果斜接长度超过miterLimit,边角就会以KCALineJoinBevel类型来显示
9.- setLineDash:count:phase:为path绘制虚线,dash数组存放各段虚线的长度,count是数组元素数量,phase是起始位置
*/
lineCapStyle // 端点类型
/*
kCGLineCapButt, // 无端点
kCGLineCapRound, // 圆形端点
kCGLineCapSquare // 方形端点(样式上和kCGLineCapButt是一样的,但是比kCGLineCapButt长一点)
*/
lineJoinStyle // 交叉点的类型
/*
kCGLineJoinMiter, // 尖角衔接
kCGLineJoinRound, // 圆角衔接
kCGLineJoinBevel // 斜角衔接
*/
-(void)drawRect:(CGRect)rect{ //设置路径线条颜色
[[UIColor redColor] setStroke]; //绘制直线
UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:CGPointMake(, )];
[path1 addLineToPoint:CGPointMake(, )];
CGFloat dash[] = {3.0, 3.0};
//这里可以设置线条为虚线,前一个数字表示虚线长度,后者表示虚线间隔。
//[path1 setLineDash:dash count:2 phase:0];
[path1 stroke]; //绘制折线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
//[path closePath];当端点>=2时,可以闭合路径.
[path stroke]; //二次贝塞尔曲线
UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointMake(, )];
[path2 addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )];
[path2 stroke]; //三次贝塞尔曲线方法
UIBezierPath *path3 = [UIBezierPath bezierPath];
[path3 moveToPoint:CGPointMake(, )];
[path3 addCurveToPoint:CGPointMake(, ) controlPoint1:CGPointMake(, ) controlPoint2:CGPointMake(, )];
[path3 stroke]; //绘制矩形
UIBezierPath *path4 = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )];
[path4 stroke]; //绘制椭圆,如果长宽相等,则绘制的就是圆形
UIBezierPath *path5 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )];
[path5 stroke]; //绘制带圆角的矩形
UIBezierPath *path6 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) cornerRadius:];
[path6 stroke]; //绘制矩形,并可指定某个角为圆角
// UIRectCornerTopLeft 左上
// UIRectCornerTopRight 右上
// UIRectCornerBottomLeft 左下
// UIRectCornerBottomRight 右下
UIBezierPath *path7 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(, )];
[path7 stroke]; //绘制圆弧
//ArcCenter圆点 radius半径 startAngle开始弧度 endAngle结束弧度 clockwise是否顺时针
UIBezierPath *path8 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, ) radius: startAngle: endAngle:M_PI_2* clockwise:YES];
[path8 stroke]; //绘制扇形
UIBezierPath *path9 = [UIBezierPath bezierPath];
[path9 moveToPoint:CGPointMake(, )];
[path9 addArcWithCenter:CGPointMake(, ) radius: startAngle: endAngle:M_PI_2 clockwise:YES];
[[UIColor redColor] setFill];//设置填充颜色
[path9 closePath];//关闭路径
[path9 fill];//设置填充
[path9 stroke];
}
若不在
-(void)drawRect:(CGRect)rect方法中使用贝塞尔曲线,直接在viewDidLoad中使用,则如下:
其它图形的绘制类似
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )]; //这里是用了CAShapeLayer,个人习惯在layer里设置线宽、断点样式、连接点样式
// path.lineWidth = 3;
// path.lineCapStyle = kCGLineCapSquare;
// path.lineJoinStyle = kCGLineJoinMiter; CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.lineWidth = ;
layer.lineCap = kCALineCapSquare;
layer.lineJoin = kCALineJoinRound;
//填充颜色
layer.fillColor = [UIColor clearColor].CGColor;
//线条填充颜色
layer.strokeColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:layer];
效果图: