ios画虚线

项目中用到了自己画的虚线,记录一下:

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        // 画虚线
        
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        
        // 设置虚线颜色为blackColor
        [shapeLayer setStrokeColor:[[UIColor colorWithHex:@"#dcd2de"] CGColor]];
        [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];
        
        // 3.0f设置虚线的宽度
        [shapeLayer setLineWidth:1.0f];
        [shapeLayer setLineJoin:kCALineJoinRound];
        
        // 8=线的宽度 3=每条线的间距
        [shapeLayer setLineDashPattern:
         [NSArray arrayWithObjects:[NSNumber numberWithInt:8],
          [NSNumber numberWithInt:6],nil]];
        
        // Setup the path
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 25, 15);
        CGPathAddLineToPoint(path, NULL, frame.size.width-25,15);
        
        [shapeLayer setPath:path];
        CGPathRelease(path);
        
        // 可以把self改成任何你想要的UIView
        [[self layer] addSublayer:shapeLayer];
        
    }
    return self;
}

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

推荐阅读更多精彩内容

  • 之前写了一篇画虚线的文章,只是做了效果,并没有对其画的过程原来深刻理解,当时只是为了业务需求。今天刚刚好遇到群里有...
    小灰是蜗牛君阅读 2,150评论 0 6
  • 前言 好几天都没有写简书了,主要是最近一直在做原型图,六天的时间出了两个项目的原型(PC+手机),结果累成狗,发现...
    Peak_One阅读 47,173评论 22 69
  • 其中的一个方法 /***画虚线@param lineView 视图@param lineLength 单个虚线大小...
    guoshengboy阅读 3,079评论 1 1
  • 使用CGContext 绘图方法 1.重写draw rect 方法: iOS 画虚线方法总结 作者有提到另外一种方...
    leonardni阅读 1,821评论 1 1
  • 这个母亲节 我们想念、我们感谢 我们忘不了妈妈烧的菜 忘不了这个来自于家的味道 小时候,妈妈烧的菜,距离是 1 扇...
    食而不惑阅读 380评论 1 5