思路
-
获取上下文
-
设置绘图信息(拼接路径)
-
将路径添加到上下文
-
把上下文渲染到视图
代码
- (void)drawRect:(CGRect)rect {
// NSLog(@"%@",NSStringFromCGRect(rect));
//1.获取上下文
//CGContextRef CG CoreGraphics Ref引用
//目前学的上下文都跟UIGraphics有关,以后想直接获取上下文,直接敲一个UIGraphics
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.设置绘图信息(拼接路径)
UIBezierPath *path=[UIBezierPath bezierPath];
//设置起点
[path moveToPoint:CGPointMake(10, 10)];
//添加一条线到某个点
[path addLineToPoint:CGPointMake(125, 125)];
[path addLineToPoint:CGPointMake(240, 10)];
//3.将路径添加到上下文,直接把UIKit的路径转换成CoreGraphics,CG开头就能转
CGContextAddPath(ctx, path.CGPath);
//4.把上下文渲染到视图,stroke描边
CGContextStrokePath(ctx);