iOS 画虚线

其中的一个方法

/**
*画虚线
@param lineView 视图
@param lineLength 单个虚线大小
@param lineSpacing 间隔
@param lineColor 虚线颜色
*/
+(void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor{

CGRect rect = CGRectMake(0, 0, CGRectGetWidth(lineView.frame), CGRectGetHeight(lineView.frame));
CGSize radii = CGSizeMake(5, 5);//圆角
UIRectCorner corners = UIRectCornerAllCorners;
//create path
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
//create shape layer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = lineColor.CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 0.5;//line的高度
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.path = path.CGPath;
shapeLayer.lineDashPattern = @[[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing]];//画虚线(虚线宽、虚线间隔)
//add it to our view
[lineView.layer addSublayer:shapeLayer];

}

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

推荐阅读更多精彩内容

  • 前言 好几天都没有写简书了,主要是最近一直在做原型图,六天的时间出了两个项目的原型(PC+手机),结果累成狗,发现...
    Peak_One阅读 47,182评论 22 69
  • 在所需要的view上画一条虚线,重写下面的方法即可 -(void)drawRect:(CGRect)rect{ [...
    旅途开发者阅读 432评论 2 0
  • 之前写了一篇画虚线的文章,只是做了效果,并没有对其画的过程原来深刻理解,当时只是为了业务需求。今天刚刚好遇到群里有...
    小灰是蜗牛君阅读 2,150评论 0 6
  • 使用CGContext 绘图方法 1.重写draw rect 方法: iOS 画虚线方法总结 作者有提到另外一种方...
    leonardni阅读 1,821评论 1 1
  • 一、萌芽于即时聊天工具的直播 从一款语音聊天软件说起。 2007年5月的一天,在任金山CEO的雷军接到周鸿祎的电话...
    猎户座公民J阅读 989评论 1 0