ios 绘制虚线

/*

** lineFrame:    虚线的 frame

** length:        虚线中短线的宽度

** spacing:      虚线中短线之间的间距

** color:        虚线中短线的颜色

*/+ (UIView*)createDashedLineWithFrame:(CGRect)lineFrame

lineLength:(int)length

lineSpacing:(int)spacing

lineColor:(UIColor*)color{UIView*dashedLine = [[UIViewalloc] initWithFrame:lineFrame];

dashedLine.backgroundColor = [UIColorclearColor];CAShapeLayer*shapeLayer = [CAShapeLayerlayer];

[shapeLayer setBounds:dashedLine.bounds];

[shapeLayer setPosition:CGPointMake(CGRectGetWidth(dashedLine.frame) /2,CGRectGetHeight(dashedLine.frame))];

[shapeLayer setFillColor:[UIColorclearColor].CGColor];

[shapeLayer setStrokeColor:color.CGColor];

[shapeLayer setLineWidth:CGRectGetHeight(dashedLine.frame)];

[shapeLayer setLineJoin:kCALineJoinRound];

[shapeLayer setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:length], [NSNumbernumberWithInt:spacing],nil]];CGMutablePathRefpath =CGPathCreateMutable();CGPathMoveToPoint(path,NULL,0,0);CGPathAddLineToPoint(path,NULL,CGRectGetWidth(dashedLine.frame),0);

[shapeLayer setPath:path];CGPathRelease(path);

[dashedLine.layer addSublayer:shapeLayer];returndashedLine;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容