美文网首页
iOS-给View添加虚线

iOS-给View添加虚线

作者: 丶奔波儿灞 | 来源:发表于2018-10-12 16:24 被阅读107次

Quartz 2D绘制

- (void)addBorderToLayer2:(UIView*)view

{

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayersetBounds:view.bounds];

    [shapeLayersetPosition:CGPointMake(CGRectGetWidth(view.frame) / 2, CGRectGetHeight(view.frame)/2)];

    [shapeLayersetStrokeColor:[UIColor lightGrayColor].CGColor];

    [shapeLayersetLineWidth:0.5];

    //  设置线宽,线间距

    [shapeLayersetLineDashPattern:@[@6,@10]];

    //  设置路径

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 0, 0);

    if (CGRectGetWidth(view.frame) > CGRectGetHeight(view.frame)) {

        CGPathAddLineToPoint(path, NULL, CGRectGetWidth(view.frame),0);

    }else{

        CGPathAddLineToPoint(path, NULL, 0,CGRectGetHeight(view.frame));

    }

    [shapeLayersetPath:path];

    CGPathRelease(path);

    //  把绘制好的虚线添加上来

    [view.layeraddSublayer:shapeLayer];

}

相关文章

网友评论

      本文标题:iOS-给View添加虚线

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