iOS 繪製虛線的三種方法
阿新 • • 發佈:2019-02-19
- 方法一:通過Quartz 2D 在 UIView drawRect:方法進行繪製虛線
- (void)drawRect:(CGRect)rect { // 可以通過 setNeedsDisplay 方法呼叫 drawRect:
// Drawing code
CGContextRef context =UIGraphicsGetCurrentContext();
// 設定線條的樣式
CGContextSetLineCap(context, kCGLineCapRound);
// 繪製線的寬度
CGContextSetLineWidth(context, 3.0 );
// 線的顏色
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
// 開始繪製
CGContextBeginPath(context);
// 設定虛線繪製起點
CGContextMoveToPoint(context, 10.0, 20.0);
// lengths的值{10,10}表示先繪製10個點,再跳過10個點,如此反覆
CGFloat lengths[] = {10,10};
// 虛線的起始點
CGContextSetLineDash(context, 0 , lengths,2);
// 繪製虛線的終點
CGContextAddLineToPoint(context, 310.0,20.0);
// 繪製
CGContextStrokePath(context);
// 關閉影象
CGContextClosePath(context);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 方法二:通過 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();
// 設定線條終點的形狀
CGContextSetLineCap(line, kCGLineCapRound);
// 設定虛線的長度 和 間距
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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 方法三:通過CAShapeLayer 方式繪製虛線
/**
* 通過 CAShapeLayer 方式繪製虛線
*
* param lineView: 需要繪製成虛線的view
* param lineLength: 虛線的寬度
* param lineSpacing: 虛線的間距
* param lineColor: 虛線的顏色
* param lineDirection 虛線的方向 YES 為水平方向, NO 為垂直方向
**/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:lineView.bounds];
if (isHorizonal) {
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
} else{
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
}
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 設定虛線顏色為blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 設定虛線寬度
if (isHorizonal) {
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
} else {
[shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
}
[shapeLayer setLineJoin:kCALineJoinRound];
// 設定線寬,線間距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
// 設定路徑
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
if (isHorizonal) {
CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
} else {
CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
}
[shapeLayer setPath:path];
CGPathRelease(path);
// 把繪製好的虛線新增上來
[lineView.layer addSublayer:shapeLayer];
}
- 方法一:通過Quartz 2D 在 UIView drawRect:方法進行繪製虛線
- (void)drawRect:(CGRect)rect { // 可以通過 setNeedsDisplay 方法呼叫 drawRect:
// Drawing code
CGContextRef context =UIGraphicsGetCurrentContext();
// 設定線條的樣式
CGContextSetLineCap(context, kCGLineCapRound);
// 繪製線的寬度
CGContextSetLineWidth(context, 3.0);
// 線的顏色
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
// 開始繪製
CGContextBeginPath(context);
// 設定虛線繪製起點
CGContextMoveToPoint(context, 10.0, 20.0);
// lengths的值{10,10}表示先繪製10個點,再跳過10個點,如此反覆
CGFloat lengths[] = {10,10};
// 虛線的起始點
CGContextSetLineDash(context, 0, lengths,2);
// 繪製虛線的終點
CGContextAddLineToPoint(context, 310.0,20.0);
// 繪製
CGContextStrokePath(context);
// 關閉影象
CGContextClosePath(context);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 方法二:通過 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();
// 設定線條終點的形狀
CGContextSetLineCap(line, kCGLineCapRound);
// 設定虛線的長度 和 間距
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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 方法三:通過CAShapeLayer 方式繪製虛線
/**
* 通過 CAShapeLayer 方式繪製虛線
*
* param lineView: 需要繪製成虛線的view
* param lineLength: 虛線的寬度
* param lineSpacing: 虛線的間距
* param lineColor: 虛線的顏色
* param lineDirection 虛線的方向 YES 為水平方向, NO 為垂直方向
**/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:lineView.bounds];
if (isHorizonal) {
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
} else{
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
}
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 設定虛線顏色為blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 設定虛線寬度
if (isHorizonal) {
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
} else {
[shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
}
[shapeLayer setLineJoin:kCALineJoinRound];
// 設定線寬,線間距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
// 設定路徑
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
if (isHorizonal) {
CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
} else {
CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
}
[shapeLayer setPath:path];
CGPathRelease(path);
// 把繪製好的虛線新增上來
[lineView.layer addSublayer:shapeLayer];
}
原文:https://blog.csdn.net/ashimar_a/article/details/53033361