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

2017-07-22  本文已影响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];

上一篇下一篇

猜你喜欢

热点阅读