美文网首页
iOS 画虚线

iOS 画虚线

作者: 漫步的老狼 | 来源:发表于2021-12-01 09:29 被阅读0次
- (void)viewDidLoad {
    [super viewDidLoad];

    UIView * view = [[UIView alloc]init];
    [self.view addSubview:view];
    _view = view;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset = 100;
        make.width.offset =100;
        make.centerX.offset = 0;
        make.height.offset = 2;
    }];
    [self.view layoutIfNeeded];
    [self drawDashLine:view lineLength:10 lineSpacing:5 lineColor:UIColor.redColor];
    
}
//画虚线
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor{
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽,线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
//    shapeLayer setLineDashPattern:<#(NSArray<NSNumber *> * _Nullable)#>
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
    
}

效果图


Simulator Screen Shot - iPhone 13 - 2021-12-01 at 09.27.47.png

相关文章

  • iOS 画虚线

    通过Quartz2D画虚线

  • iOS 画虚线

    创建一个UIView的子类,在drawRect:(CGRect)rect里

  • iOS 画虚线

    在所需要的view上画一条虚线,重写下面的方法即可 -(void)drawRect:(CGRect)rect{ [...

  • IOS画虚线

    UIImageView*imageView1 = [[UIImageViewalloc]initWithFrame...

  • ios画虚线

    项目中用到了自己画的虚线,记录一下:

  • iOS 画虚线

    其中的一个方法 /***画虚线@param lineView 视图@param lineLength 单个虚线大小...

  • iOS 画虚线

    效果图

  • iOS 画线

    1.iOS画虚线边框layer.border 2.iOS画小于1px的细线 3.iOS 设置行距,并且计算有行距的...

  • iOS画虚线 画实线

    // 画虚线- (void)drawRect:(CGRect)rect { CAShapeLayer *dott...

  • ios-画虚线

    需求: 需要实现虚线功能 实现方法: 方法一:使用贝塞尔曲线实现 方法二:Quartz 2D绘制

网友评论

      本文标题:iOS 画虚线

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