iOS 技术分享

iOS - 绘制虚线

2020-04-10  本文已影响0人  Joh蜗牛
方法封装:
- (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];

    //  设置虚线颜色
    [shapeLayer setStrokeColor:lineColor.CGColor];

    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];

    //  设置线宽,线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength],[NSNumber numberWithInt:lineSpacing], nil]];

    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);

    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}
调取使用:
[self drawDashLine:self.lineView lineLength:10 lineSpacing:5 lineColor:kColorHex(0xEDEDED)];

1.参数self.lineView:虚线所在view;
2.参数lineLength:虚线中实线的长度;
3.参数lineSpacing:虚线中每个实现的间距;

上一篇 下一篇

猜你喜欢

热点阅读