画虚线

作者: averroe | 来源:发表于2017-02-15 17:53 被阅读21次

    - (void)drawLine

    {

    UIView*lineView = [[UIViewalloc]init];

    lineView.backgroundColor= [UIColorwhiteColor];

    lineView.frame=CGRectMake(10,50,kScreenWidth-50,0.5);

    [selfaddSubview:lineView];

    CAShapeLayer*shapeLayer = [CAShapeLayerlayer];

    [shapeLayersetBounds:lineView.bounds];

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

    [shapeLayersetFillColor:[UIColorclearColor].CGColor];

    //设置虚线颜色为blackColor

    //[shapeLayer setStrokeColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0].CGColor];

    [shapeLayersetStrokeColor:[UIColorlightGrayColor].CGColor];

    //设置虚线宽度

    [shapeLayersetLineWidth:CGRectGetHeight(lineView.frame)];

    [shapeLayersetLineJoin:kCALineJoinRound];

    //设置线宽,线间距

    [shapeLayersetLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:3],[NSNumbernumberWithInt:1],nil]];

    //设置路径

    CGMutablePathRefpath =CGPathCreateMutable();

    CGPathMoveToPoint(path,NULL,0,0);

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

    [shapeLayersetPath:path];

    CGPathRelease(path);

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

    [lineView.layer addSublayer:shapeLayer];

    }


    相关文章

      网友评论

          本文标题:画虚线

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