在RootView.m里重写如下方法
- (void)drawRect:(CGRect)rect
{
// 初始化一个路线
// self.path = [UIBezierPath bezierPath]; // 点之间的过度效果
// _path.lineCapStyle = kCGLineCapRound; // 设置节点过度效果
// _path.lineJoinStyle = kCGLineJoinRound; // 设置终点过度效果
// _path.lineWidth = 3.0; // 设置线宽
//
// [_path moveToPoint:CGPointMake(100, 100)];
//
// [_path addLineToPoint:CGPointMake(150, 150)];
// [path addLineToPoint:CGPointMake(180, 130)];
// [path addLineToPoint:CGPointMake(200, 170)];
// [path addLineToPoint:CGPointMake(120, 180)];
//
// [path stroke];
// 得到上下文 1 在drawRect拿到上下文 2 通过一个image图片拿到上下文
// CGContextRef context = UIGraphicsGetCurrentContext();
// // 设置了画笔的颜色
// CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// // 设置画笔的粗细
// CGContextSetLineWidth(context, 2.0);
for (UIBezierPath *path in self.paths) {
[path stroke];
}
}
- (NSMutableArray *)paths
{
if (!_paths) {
_paths = [NSMutableArray array];
}
return _paths;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取touch对象
UITouch *touch = [touches anyObject];
// 获取touch起始点
CGPoint point = [touch locationInView:touch.view];
// 创建路径对象
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 3.0;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
// 设置起点
[path moveToPoint:point];
// 添加路径
[self.paths addObject:path];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取touch对象
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
UIBezierPath *path = [self.paths lastObject];
[path addLineToPoint:point];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}