畫虛線需要用到函數:
CGContextSetLineDash
此函數需要四個參數:
- context – 這個不用多說
- phase - 稍後再說
- lengths – 指明虛線是如何交替繪製,具體看例子
- count – lengths數組的長度
- CGContextRef context =UIGraphicsGetCurrentContext();
- CGContextBeginPath(context);
- CGContextSetLineWidth(context, 2.0);
- CGContextSetStrokeColorWithColor(context, [UIColorwhiteColor].CGColor);
- float lengths[] = {10,10};
- CGContextSetLineDash(context, 0, lengths,2);
- CGContextMoveToPoint(context, 10.0, 20.0);
- CGContextAddLineToPoint(context, 310.0,20.0);
- CGContextStrokePath(context);
- CGContextClosePath(context);
lengths的值{10,10}表示先繪製10個點,再跳過10個點,如此反覆,如圖:
如果把lengths值改爲{10, 20, 10},則表示先繪製10個點,跳過20個點,繪製10個點,跳過10個點,再繪製20個點,如此反覆,如圖:
注意count的值等於lengths數組的長度
phase參數表示在第一個虛線繪製的時候跳過多少個點,舉例說明:
- float lengths[] = {10,5};
- CGContextSetLineDash(context, 0, lengths, 2);
- CGContextMoveToPoint(context, 0.0, 20.0);
- CGContextAddLineToPoint(context, 310.0, 20.0);
- CGContextStrokePath(context);
- CGContextSetLineDash(context, 5, lengths, 2);
- CGContextMoveToPoint(context, 0.0, 40.0);
- CGContextAddLineToPoint(context, 310.0, 40.0);
- CGContextStrokePath(context);
- CGContextSetLineDash(context, 8, lengths, 2);
- CGContextMoveToPoint(context, 0.0, 60.0);
- CGContextAddLineToPoint(context, 310.0, 60.);
- CGContextStrokePath(context);
由於lengths值爲{10,5},第一條線就是繪製10,跳過5,反覆繪製。
第二條線的phase值爲5,則首先繪製【10減去5】,再跳過5,繪製10,反覆繪製。
第三條給也如此,先繪製2,再跳過5,如此反覆。
1.image中畫線方法
- (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.view.frame]; [self.view addSubview:imageView]; self.view.backgroundColor=[UIColor blueColor]; UIGraphicsBeginImageContext(imageView.frame.size); [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //邊緣樣式 CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); //線寬 CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); //顏色 CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100, 100); //起點座標 CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200, 100); //終點座標 CGContextStrokePath(UIGraphicsGetCurrentContext()); imageView.image=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }
2.獲取當前Context畫線方法:(未主動創建path)
CGContextRef ctx = UIGraphicsGetCurrentContext();//獲取當前ctx
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(ctx, 15.0); //線寬
CGContextSetAllowsAntialiasing(ctx, YES);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); //顏色
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 100, 100); //起點座標
CGContextAddLineToPoint(ctx, 200, 100); //終點座標
CGContextStrokePath(ctx);
//畫兩條射線
// 創建一個Path句柄
CGMutablePathRef pathRef = CGPathCreateMutable();
// 初始化該path到一個初始點
CGPathMoveToPoint(pathRef, &CGAffineTransformIdentity, 100.0f, 0.0f);
// 添加一條直線,從初始點到該函數指定的座標點
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, 150.0f, 100.0f);
CGPathMoveToPoint(pathRef, &CGAffineTransformIdentity, 100.0f, 0.0f);
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, 100.0f, 150.0f);
CGPathCloseSubpath(pathRef);
// 關閉該path
CGPathCloseSubpath(pathRef);
// 將此path添加到Quartz上下文中
CGContextAddPath(ctx, pathRef);
// 對上下文進行描邊
CGContextStrokePath(ctx);
//畫三角形
//CGMutablePathRef pathRef = CGPathCreateMutable();
// 初始化該path到一個初始點
CGPathMoveToPoint(pathRef, &CGAffineTransformIdentity, 0.0f, 0.0f);
// 添加一條直線,從初始點到該函數指定的座標點
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, 50.0f, 100.0f);
CGPathAddLineToPoint(pathRef, &CGAffineTransformIdentity, 100.0f, 50.0f);
// 關閉該path
CGPathCloseSubpath(pathRef);
// 關閉該path
CGPathCloseSubpath(pathRef);
// 將此path添加到Quartz上下文中
CGContextAddPath(ctx, pathRef);
// 對上下文進行描邊
CGContextStrokePath(ctx);