ios 绘制虚线

/*

** lineFrame:    虚线的 frame

** length:        虚线中短线的宽度

** spacing:      虚线中短线之间的间距

** color:        虚线中短线的颜色

*/+ (UIView*)createDashedLineWithFrame:(CGRect)lineFrame

lineLength:(int)length

lineSpacing:(int)spacing

lineColor:(UIColor*)color{UIView*dashedLine = [[UIViewalloc] initWithFrame:lineFrame];

dashedLine.backgroundColor = [UIColorclearColor];CAShapeLayer*shapeLayer = [CAShapeLayerlayer];

[shapeLayer setBounds:dashedLine.bounds];

[shapeLayer setPosition:CGPointMake(CGRectGetWidth(dashedLine.frame) /2,CGRectGetHeight(dashedLine.frame))];

[shapeLayer setFillColor:[UIColorclearColor].CGColor];

[shapeLayer setStrokeColor:color.CGColor];

[shapeLayer setLineWidth:CGRectGetHeight(dashedLine.frame)];

[shapeLayer setLineJoin:kCALineJoinRound];

[shapeLayer setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:length], [NSNumbernumberWithInt:spacing],nil]];CGMutablePathRefpath =CGPathCreateMutable();CGPathMoveToPoint(path,NULL,0,0);CGPathAddLineToPoint(path,NULL,CGRectGetWidth(dashedLine.frame),0);

[shapeLayer setPath:path];CGPathRelease(path);

[dashedLine.layer addSublayer:shapeLayer];returndashedLine;

}

你可能感兴趣的:(ios 绘制虚线)