iOS開發(fā)繪制虛線的方法
方法一:通過Quartz 2D 在 UIView drawRect:方法進(jìn)行繪制虛線
- (void)drawRect:(CGRect)rect {// 可以通過 setNeedsDisplay 方法調(diào)用 drawRect:
????// Drawing code
????CGContextRef context =UIGraphicsGetCurrentContext();
????// 設(shè)置線條的樣式
????CGContextSetLineCap(context, kCGLineCapRound);
????// 繪制線的寬度
????CGContextSetLineWidth(context,3.0);
????// 線的顏色
????CGContextSetStrokeColorWithColor(context, [UIColororangeColor].CGColor);
????// 開始繪制
????CGContextBeginPath(context);
????// 設(shè)置虛線繪制起點
????CGContextMoveToPoint(context,10.0,20.0);
????// lengths的值{10,10}表示先繪制10個點,再跳過10個點,如此反復(fù)
????CGFloat lengths[] = {10,10};
????// 虛線的起始點
????CGContextSetLineDash(context,0, lengths,2);
????// 繪制虛線的終點
????CGContextAddLineToPoint(context,310.0,20.0);
????// 繪制
????CGContextStrokePath(context);
????// 關(guān)閉圖像
????CGContextClosePath(context);
}
方法二:通過 Quartz 2D 在 UIImageView 繪制虛線
/**
*? 通過 Quartz 2D 在 UIImageView 繪制虛線
*
*? param imageView 傳入要繪制成虛線的imageView
*? return
*/
- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
????// 開始劃線 劃線的frame
????UIGraphicsBeginImageContext(imageView.frame.size);? ??
????[imageView.image drawInRect:CGRectMake(0,0, imageView.frame.size.width, imageView.frame.size.height)];
????// 獲取上下文
????CGContextRef line = UIGraphicsGetCurrentContext();
????// 設(shè)置線條終點的形狀
????CGContextSetLineCap(line, kCGLineCapRound);
????// 設(shè)置虛線的長度 和 間距
????CGFloat lengths[] = {5,5};? ?
?????CGContextSetStrokeColorWithColor(line, [UIColor greenColor].CGColor);
????// 開始繪制虛線
????CGContextSetLineDash(line,0, lengths,2);? ?
?????CGContextMoveToPoint(line,0.0,2.0);? ?
?????CGContextAddLineToPoint(line,300,2.0);? ?
?????CGContextStrokePath(line);
????// UIGraphicsGetImageFromCurrentImageContext()返回的就是image
????return UIGraphicsGetImageFromCurrentImageContext();
}
方法三:通過CAShapeLayer 方式繪制虛線
/**
** lineView:? ? ? 需要繪制成虛線的view
** lineLength:? ? 虛線的寬度
** lineSpacing:? ? 虛線的間距
** lineColor:? ? ? 虛線的顏色
**/
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
? ? CAShapeLayer *shapeLayer = [CAShapeLayer layer];
? ? [shapeLayer setBounds:lineView.bounds];
? ? [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
? ? [shapeLayer setFillColor:[UIColor clearColor].CGColor];
? ? //? 設(shè)置虛線顏色
? ? [shapeLayer setStrokeColor:lineColor.CGColor];
? ? //? 設(shè)置虛線寬度
? ? [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
? ? [shapeLayer setLineJoin:kCALineJoinRound];
? ? //? 設(shè)置線寬欢顷,線間距
? ? [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
? ? //? 設(shè)置路徑
? ? CGMutablePathRef path = CGPathCreateMutable();
? ? CGPathMoveToPoint(path, NULL, 0, 0);
? ? CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
? ? [shapeLayer setPath:path];
? ? CGPathRelease(path);
? ? //? 把繪制好的虛線添加上來
? ? [lineView.layer addSublayer:shapeLayer];
}