iOS虚线,为您的应用增加美感

2020-12-23 10:59:48 浏览数 (1)

良好的虚线和圆角视觉效果,会为一个好的app增加美感。iOS虚线,一般是用CAShapeLayer设置其lineWidth、lineDashPattern等属性,然后加载到容器里面,下面是封装好的函数,仅供参考。

代码语言:javascript复制
- (UIView *)addImaginaryLineWithFrame:(CGRect)frame lineColor:(UIColor *)color lineHeight:(float)height lineDashWidth:(NSNumber *)width lineDashSpace:(NSNumber *)space {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.position = CGPointMake(0, 1);
    shapeLayer.fillColor = nil;
    
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.lineWidth = height;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineDashPattern = @[width, space];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 10, 0);
    CGPathAddLineToPoint(path, NULL, kScreenWidth - 10,0);
    shapeLayer.path = path;
    CGPathRelease(path);
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view.layer addSublayer:shapeLayer];
    return view;
}

0 人点赞