美文网首页
iOS 生成虚线

iOS 生成虚线

作者: tongyuling | 来源:发表于2019-09-26 20:18 被阅读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(0x999999).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];
    }
    
    

    注:如果你Masonry中使用虚线,如下操作:

    -(void)layoutSublayersOfLayer:(CALayer *)layer{
        
        [super layoutSublayersOfLayer:layer];
        //虚线
        [self showLine:self.view];
    }
    

    相关文章

      网友评论

          本文标题:iOS 生成虚线

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