iOS边框虚线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CAShapeLayer *border = [CAShapeLayer layer];
//虚线的颜色
border.strokeColor = [UIColor greenColor].CGColor;
//填充的颜色
border.fillColor = [UIColor clearColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.btn.bounds cornerRadius:5];
//设置路径
border.path = path.CGPath;
border.frame = self.btn.bounds;
//虚线的宽度
border.lineWidth = 3;
//虚线的间隔4线段长度,2线段与线段间距
border.lineDashPattern = @[@10, @5];
self.btn.layer.cornerRadius = 5.f;
self.btn.layer.masksToBounds = YES;
[self.btn.layer addSublayer:border];