良好的虛線和圓角視覺(jué)效果实愚,會(huì)為一個(gè)好的app增加美感兼呵。iOS虛線兔辅,一般是用CAShapeLayer設(shè)置其lineWidth、lineDashPattern等屬性击喂,然后加載到容器里面维苔,下面是封裝好的函數(shù),僅供參考懂昂。
- (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;
}