·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 画虚线

画虚线

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23
 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 }