美文网首页iOS 深度好文
iOS虚线,为您的应用增加美感

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

作者: sweetpf | 来源:发表于2017-08-18 10:25 被阅读111次

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

- (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;
}

相关文章

网友评论

    本文标题:iOS虚线,为您的应用增加美感

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