- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
[_lineArry addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 获取当前点的位置
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(point));
// 拿到最后一个对象
NSMutableArray *pointArray = [_lineArry lastObject];
// 转换成对象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
// 添加到数组对象中
[pointArray addObject:pointValue];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// 拿到绘图的配置信息(绘画的属性信息)
// 通过图片得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// 设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
for (int i = 0; i < [_lineArry count]; i++) {
NSMutableArray *pointArry = [_lineArry objectAtIndex:i];
for (int j = 0; j < (int)pointArry.count-1; j++) {
NSValue *firstPointValue = [pointArry objectAtIndex:j];
NSValue *secondPointValue = [pointArry objectAtIndex:j + 1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
// 把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// 笔触和另一点形成一个路径(从move到point)
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}// 绘制图形
CGContextStrokePath(context);
}
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
[_lineArry addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 获取当前点的位置
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(point));
// 拿到最后一个对象
NSMutableArray *pointArray = [_lineArry lastObject];
// 转换成对象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
// 添加到数组对象中
[pointArray addObject:pointValue];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// 拿到绘图的配置信息(绘画的属性信息)
// 通过图片得到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// 设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
for (int i = 0; i < [_lineArry count]; i++) {
NSMutableArray *pointArry = [_lineArry objectAtIndex:i];
for (int j = 0; j < (int)pointArry.count-1; j++) {
NSValue *firstPointValue = [pointArry objectAtIndex:j];
NSValue *secondPointValue = [pointArry objectAtIndex:j + 1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
// 把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// 笔触和另一点形成一个路径(从move到point)
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}// 绘制图形
CGContextStrokePath(context);
}