-(void)showLine:(UIView *)view
{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:view.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(view.frame) / 2, CGRectGetHeight(view.frame))];
//设置虚线颜色
[shapeLayer setStrokeColor:HEXCOLOR(0x999999).CGColor];
shapeLayer.lineWidth = 0.5;
//设置虚线的线宽及间距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:2], nil]];
//创建虚线绘制路径
CGMutablePathRef path = CGPathCreateMutable();
//设置虚线绘制路径起点
CGPathMoveToPoint(path, NULL, 0, 0);
//设置虚线绘制路径终点
CGPathAddLineToPoint(path, NULL, CGRectGetWidth(view.frame), 0);
//设置虚线绘制路径
[shapeLayer setPath:path];
CGPathRelease(path);
//添加虚线
[view.layer addSublayer:shapeLayer];
}
注:如果你Masonry中使用虚线,如下操作:
-(void)layoutSublayersOfLayer:(CALayer *)layer{
[super layoutSublayersOfLayer:layer];
//虚线
[self showLine:self.view];
}
网友评论