1 + (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
2 {
3 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
4 [shapeLayer setBounds:lineView.bounds];
5 [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
6 [shapeLayer setFillColor:[UIColor clearColor].CGColor];
7 // 设置虚线颜色
8 [shapeLayer setStrokeColor:lineColor.CGColor];
9 // 设置虚线宽度
10 [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
11 [shapeLayer setLineJoin:kCALineJoinRound];
12 // 设置线宽,线间距
13 [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
14 // 设置路径
15 CGMutablePathRef path = CGPathCreateMutable();
16 CGPathMoveToPoint(path, NULL, 0, 0);
17 CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
18 [shapeLayer setPath:path];
19 CGPathRelease(path);
20 // 把绘制好的虚线添加上来
21 [lineView.layer addSublayer:shapeLayer];
22 }