iOS两种绘制虚线的方法
2017-03-29 本文已影响86人
子疯zp
我是虚线哦!.jpg
控件绘制虚线.png
个人CSND
一、绘制单条的虚线
二、给一个控件添加虚线
1、绘制单条的虚线
/**
** lineView: 需要绘制成虚线的view
** lineLength: 虚线的宽度 //2
** lineSpacing: 虚线的间距//1
** 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 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:-------
绘制单条虚线.jpeg2、给一个控件添加虚线
+ (void)drawLineForView:(UIView *)view{
CAShapeLayer *border = [CAShapeLayer layer];
border.strokeColor = [UIColor blackColor].CGColor;
border.fillColor = nil;
border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
border.frame = view.bounds;
border.lineWidth = 1.f;
border.lineCap = @"square";
border.lineDashPattern = @[@4, @2];
[view.layer addSublayer:border];
}
样式2:
控件绘制虚线.png