注:本篇文章都要写在UIView
的-drawRect:
方法里。
1. 直线
- (void)drawRect:(CGRect)rect
{
[[UIColor redColor] set];
{// 1. 直线
UIBezierPath *path = [[UIBezierPath alloc] init]; //初始化也可以用 [UIBezierPath bezierPath];
path.lineWidth = 3.f;
path.lineCapStyle = kCGLineCapRound; //端点样式(圆形)
path.lineJoinStyle = kCGLineJoinRound; //连接类型
path.miterLimit = 10.f;
path.flatness = 10.f; //绘线的精细程度,默认为0.6,数值越大,需要处理的时间越长
path.usesEvenOddFillRule = YES;
// 设置起始点
[path moveToPoint:CGPointMake(30, 30)];
// 添加子路径
[path addLineToPoint:CGPointMake(100, 20)];//添加一条子路径
[path addLineToPoint:CGPointMake(200, 50)];//添加一条子路径
[path closePath]; //当构建子路径数>=2条时,可以调用`closePath`方法来闭合路径.
// 根据坐标点连线
[path stroke]; //各个点连线
}
2. 矩形、圆角矩形
// 2.矩形,可以用3个 addLineToPoint: 方法,也可以直接用下面方法:
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 70, 50, 50)];
[path fill]; //设置填充
[path stroke]; //各个点连线
// 3.圆角矩形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(120, 70, 50, 50)
cornerRadius:10];
[path1 stroke];
UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(220, 70, 50, 50)
byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight
cornerRadii:CGSizeMake(10, 10)];
[path2 stroke];
3. 椭圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 140, 100, 50)];
[path stroke];
4. 圆弧
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 200)
radius:50
startAngle:0
endAngle:M_PI/2
clockwise:YES];
[path fill];
[path stroke];
5. 扇形
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 260)];
[path addArcWithCenter:CGPointMake(200, 260) radius:50 startAngle:0 endAngle:M_PI/2 clockwise:YES];
[path closePath]; //形成闭环
[path stroke];
6. 虚线
UIBezierPath *path = [UIBezierPath bezierPath];
//椭圆虚线
//UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 200)];
//圆虚线
//UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:40];
CGFloat dash[] = {3.0, 3.0};
[path setLineDash:dash //各段虚线长度
count:2 //数组数量
phase:0.0]; //起始位置
[path moveToPoint:CGPointMake(20, 320)];
[path addLineToPoint:CGPointMake(200, 320)];
[path stroke];
7. 二次贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 360)];
[path addQuadCurveToPoint:CGPointMake(200, 360) //终点
controlPoint:CGPointMake(100, 320)]; //控制点
[path stroke];
8. 三次贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 400)];
[path addCurveToPoint:CGPointMake(300, 400) //终点
controlPoint1:CGPointMake(100, 350) //控制点
controlPoint2:CGPointMake(200, 450)]; //控制点
[path stroke];
9. 用CAShapeLayer
设置边框颜色
UIBezierPath *pathA = [UIBezierPath bezierPathWithRect:CGRectMake(100, 480, 100, 100)];
CAShapeLayer *layerA = [[CAShapeLayer alloc] init];
layerA.lineWidth = 5.0f;
layerA.strokeColor = [UIColor orangeColor].CGColor; //线色
layerA.fillColor = [UIColor greenColor].CGColor; //背景色
[self.layer addSublayer:layerA];
layerA.path = pathA.CGPath;
10. 用CAShapeLayer
加阴影
CGRect frame = CGRectMake(260, 260, 50, 50);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:frame];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillColor = [UIColor greenColor].CGColor;
shapeLayer.shadowOffset = CGSizeMake(5, 5);
shapeLayer.shadowColor = [UIColor blackColor].CGColor;
shapeLayer.shadowOpacity = 0.5;
[self.layer addSublayer:shapeLayer];
shapeLayer.path = path.CGPath;