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

swift

let shapeLayer:CAShapeLayer = CAShapeLayer()
        shapeLayer.frame = CGRect.init(x: 0, y: 1, width: kScreenW - 30, height: 2)
        shapeLayer.fillColor = UIColor.clear.cgColor
        //设置虚线颜色
        shapeLayer.strokeColor = kMainLightGrayColor.cgColor
        //设置虚线宽度
        shapeLayer.lineWidth = 1
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPhase = 0
        //设置虚线的线宽及间距
        shapeLayer.lineDashPattern = [NSNumber(value: 5), NSNumber(value: 5)]
        //创建虚线绘制路径
        let path:CGMutablePath = CGMutablePath()
        //设置虚线绘制路径起点
        path.move(to: CGPoint(x: 0, y: 0))
        //设置虚线绘制路径终点
        path.addLine(to: CGPoint(x: kScreenW - 30, y: 0))
        //设置虚线绘制路径
        shapeLayer.path = path
        //添加虚线
        line.layer.addSublayer(shapeLayer)
绘制虚线
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];

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

推荐阅读更多精彩内容