画虚线需要用到函数:
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);