#import "DoodleView.h"
@implementation DoodleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//创建一个大数组用来保存画过的线
self._lineArray = [NSMutableArray arrayWithCapacity:1];
//创建撤销按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake((frame.size.width - 100) / 2, 430, 100, 30);
[button setTitle:@"撤销" forState:UIControlStateNormal];
[button addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
-(void)undo:(UIButton *)button
{
[__lineArray removeLastObject];
//重绘界面
[self setNeedsDisplay];
}
//要绘图必须重写这个方法
- (void)drawRect:(CGRect)rect
{
//拿到VIEW的上下文,context用来得到绘图中的一些设置信息
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔颜色
CGContextSetStrokeColorWithColor(context,[UIColor yellowColor].CGColor);
//设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
//拿到每一笔中的小数组
for (int i = 0; i < self._lineArray.count; i++) {
NSMutableArray *arrayPoint = [__lineArray objectAtIndex:i];
//在建一个循环,拿到小数组每一个点
for (int j = 0; j < (int)arrayPoint.count - 1; j++) {
//先拿到第一个点和第二个点
NSValue *firstPointValue = [arrayPoint objectAtIndex:j];
NSValue *secondPointValue = [arrayPoint objectAtIndex:j + 1];
//将两个转化成CGPoint类型
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
//把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
//将第二个点的笔触与第一个连成一个路径
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}
//绘制路径
CGContextStrokePath(context);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//创建一个小数组用来记录每一条线,将每一条线放入大数组保存
NSMutableArray *arrayPoint = [NSMutableArray arrayWithCapacity:1];
[__lineArray addObject:arrayPoint];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//取得手指移动的位置
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//将手指的点透过大数组来放入小数组中
//取得小数组,每次加入可变数组的元素都在最后一位
NSMutableArray *arrayPoint = [__lineArray lastObject];
// [arrayPoint addObject:point];数组只能放对象类型,所以想把Point放入数组必须先转化成对 象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
//将点加入数组中保存
[arrayPoint addObject:pointValue];
//重绘界面
[self setNeedsDisplay];
// NSLog(@"point = %@",NSStringFromCGPoint(point));
}
@end
@implementation DoodleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//创建一个大数组用来保存画过的线
self._lineArray = [NSMutableArray arrayWithCapacity:1];
//创建撤销按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake((frame.size.width - 100) / 2, 430, 100, 30);
[button setTitle:@"撤销" forState:UIControlStateNormal];
[button addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
-(void)undo:(UIButton *)button
{
[__lineArray removeLastObject];
//重绘界面
[self setNeedsDisplay];
}
//要绘图必须重写这个方法
- (void)drawRect:(CGRect)rect
{
//拿到VIEW的上下文,context用来得到绘图中的一些设置信息
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔颜色
CGContextSetStrokeColorWithColor(context,[UIColor yellowColor].CGColor);
//设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
//拿到每一笔中的小数组
for (int i = 0; i < self._lineArray.count; i++) {
NSMutableArray *arrayPoint = [__lineArray objectAtIndex:i];
//在建一个循环,拿到小数组每一个点
for (int j = 0; j < (int)arrayPoint.count - 1; j++) {
//先拿到第一个点和第二个点
NSValue *firstPointValue = [arrayPoint objectAtIndex:j];
NSValue *secondPointValue = [arrayPoint objectAtIndex:j + 1];
//将两个转化成CGPoint类型
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValue CGPointValue];
//把笔触移动到一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
//将第二个点的笔触与第一个连成一个路径
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}
//绘制路径
CGContextStrokePath(context);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//创建一个小数组用来记录每一条线,将每一条线放入大数组保存
NSMutableArray *arrayPoint = [NSMutableArray arrayWithCapacity:1];
[__lineArray addObject:arrayPoint];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//取得手指移动的位置
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//将手指的点透过大数组来放入小数组中
//取得小数组,每次加入可变数组的元素都在最后一位
NSMutableArray *arrayPoint = [__lineArray lastObject];
// [arrayPoint addObject:point];数组只能放对象类型,所以想把Point放入数组必须先转化成对 象类型
NSValue *pointValue = [NSValue valueWithCGPoint:point];
//将点加入数组中保存
[arrayPoint addObject:pointValue];
//重绘界面
[self setNeedsDisplay];
// NSLog(@"point = %@",NSStringFromCGPoint(point));
}
@end