美文网首页
iOS view添加虚线效果

iOS view添加虚线效果

作者: tongyuling | 来源:发表于2020-02-25 17:20 被阅读0次
-(void)showLine:(UIView *)view
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:view.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(view.frame) / 2, CGRectGetHeight(view.frame))];
    //设置虚线颜色
    [shapeLayer setStrokeColor:HEXCOLOR(0xCCCCCC).CGColor];
    shapeLayer.lineWidth = 0.5;
    //设置虚线的线宽及间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:2], nil]];
    //创建虚线绘制路径
    CGMutablePathRef path = CGPathCreateMutable();
    //设置虚线绘制路径起点
    CGPathMoveToPoint(path, NULL, 0, 0);
    //设置虚线绘制路径终点
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(view.frame), 0);
    //设置虚线绘制路径
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //添加虚线
    [view.layer addSublayer:shapeLayer];
}

相关文章

网友评论

      本文标题:iOS view添加虚线效果

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