虚线全集
2018-07-31 本文已影响2人
山水域
画虚线
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 1)];
[self.view addSubview:line];
//绘制虚线
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:line.bounds];
[shapeLayer setPosition:CGPointMake(line.frame.size.width / 2.0,line.frame.size.height)];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
//设置虚线颜色
[shapeLayer setStrokeColor:[UIColor blackColor].CGColor];
//设置虚线宽度
[shapeLayer setLineWidth:0.5];
[shapeLayer setLineJoin:kCALineJoinRound];
//设置虚线的线宽及间距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:2], nil]];
//创建虚线绘制路径
CGMutablePathRef path = CGPathCreateMutable();
//设置虚线绘制路径起点
CGPathMoveToPoint(path, NULL, 0, 0);
//设置虚线绘制路径终点
CGPathAddLineToPoint(path, NULL, line.frame.size.width, 0);
//设置虚线绘制路径
[shapeLayer setPath:path];
CGPathRelease(path);
//添加虚线
[line.layer addSublayer:shapeLayer];
画线框
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, self.view.frame.size.width - 40, 30)];
[self.view addSubview:imageView];
//给控件边缘加虚线
CAShapeLayer *border = [CAShapeLayer layer];
border.strokeColor = [UIColor redColor].CGColor;
border.fillColor = nil;
border.path = [UIBezierPath bezierPathWithRect:imageView.bounds].CGPath;
border.frame = imageView.bounds;
border.lineWidth = .5f;
border.lineCap = @"square";
border.lineDashPattern = @[@4, @2];
[imageView.layer addSublayer:border];