美文网首页
iOS开发笔记-48: 绘制虚线相关

iOS开发笔记-48: 绘制虚线相关

作者: 原味蛋炒饭 | 来源:发表于2017-07-22 16:50 被阅读142次
    绘制虚线
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setBounds:_line2.bounds];
        [shapeLayer setPosition:CGPointMake(_line2.frame.size.width / 2.0,_line2.frame.size.height)];
        [shapeLayer setFillColor:[UIColor clearColor].CGColor];
        //设置虚线颜色
        [shapeLayer setStrokeColor:_kmainLightGrayColor.CGColor];
        //设置虚线宽度
        [shapeLayer setLineWidth:0.5];
        [shapeLayer setLineJoin:kCALineJoinRound];
        //设置虚线的线宽及间距
        [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber       numberWithInt:5], [NSNumber numberWithInt:2], nil]];
        //创建虚线绘制路径
        CGMutablePathRef path = CGPathCreateMutable();
        //设置虚线绘制路径起点
        CGPathMoveToPoint(path, NULL, 0, 0);
        //设置虚线绘制路径终点
        CGPathAddLineToPoint(path, NULL, _line2.frame.size.width, 0);
        //设置虚线绘制路径
        [shapeLayer setPath:path];
        CGPathRelease(path);
        //添加虚线
        [_line2.layer addSublayer:shapeLayer];
    
    
    //给控件边缘加虚线
    CAShapeLayer *border = [CAShapeLayer layer];
        border.strokeColor = [UIColor blackColor].CGColor;
        border.fillColor = nil;
        border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
        border.frame = view.bounds;
        border.lineWidth = 1.f;
        border.lineCap = @"square";
        border.lineDashPattern = @[@4, @2];
        [view.layer addSublayer:border];
    
    

    相关文章

      网友评论

          本文标题:iOS开发笔记-48: 绘制虚线相关

          本文链接:https://www.haomeiwen.com/subject/odyukxtx.html