绘制虚线

参考原文

绘制虚线方法

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

使用

1
2
3
4
[ViewController drawDashLine:self.lineView
lineLength:10
lineSpacing:2.0
lineColor:[UIColor colorWithHexString:@"393939"]];

绘制虚线【竖线】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 绘制虚线【竖线】
*
* @param lineView 需要绘制成虚线的view
* @param lineLength 虚线的宽度
* @param lineSpacing 虚线的间距
* @param lineColor 虚线的颜色
*/
+ (void)drawDashVerticalLine:(UIView *)lineView
lineLength:(int)lineLength
lineSpacing:(int)lineSpacing
lineColor:(UIColor *)lineColor {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame),
CGRectGetHeight(lineView.frame) * 0.5)];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:lineLength];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer
setLineDashPattern:[NSArray
arrayWithObjects:[NSNumber
numberWithInt:lineLength],
[NSNumber
numberWithInt:lineSpacing],
nil]];
// 设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, 0, lineView.frame.size.height);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layer addSublayer:shapeLayer];
}

category下载