天天看點

iOS虛線,為您的應用增加美感

良好的虛線和圓角視覺效果,會為一個好的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;
}           

複制