//创建一个类RootView继承于UIview
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.lineArray = [NSMutableArray arrayWithCapacity:1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 600, 100, 40);
[button setTitle:@"撤销" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(120, 600, 100, 40);
[button1 setTitle:@"重新开始" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(button1DidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button1];
UIButton *button11 = [UIButton buttonWithType:UIButtonTypeSystem];
button11.frame = CGRectMake(220, 600, 100, 40);
[button11 setTitle:@"保存" forState:UIControlStateNormal];
[button11 addTarget:self action:@selector(button11DidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button11];
}
return self;
}
-(void)buttonDidClicked:(UIButton *)sender{
[_lineArray removeLastObject];
[self setNeedsDisplay];
}
-(void)button1DidClicked:(UIButton *)sender{
[_lineArray removeAllObjects];
[self setNeedsDisplay];
}
-(void)button11DidClicked:(UIButton *)sender{
UIImageWriteToSavedPhotosAlbum([self convertViewToImage:self], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
[_lineArray removeAllObjects];
[self setNeedsDisplay];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
//吧存放点的数组放进一个大的数组中 相当于用一个大的数组来存放所有用点来组成的线
[_lineArray addObject:pointArray];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@",NSStringFromCGPoint(point));
NSMutableArray *pointArray = [_lineArray lastObject];
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[pointArray addObject:pointValue];
//重绘界面
[self setNeedsDisplay];
}
//涂鸦的方法
-(void)drawRect:(CGRect)rect{
//得到上下文 1 在drawrect拿到上下文 2 通过一个Image图片拿到上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//设置画笔的粗细
CGContextSetLineWidth(context, 2.0);
for (int i = 0; i < [_lineArray count]; i++) {
NSMutableArray *pointArray = [_lineArray objectAtIndex:i];
for (int j = 0; j <(int)pointArray.count - 1; j++) {
//拿出小数组之中的两个点
NSValue *firstPointValue = [pointArray objectAtIndex:j];
NSValue *secondPointValue = [pointArray objectAtIndex:j+1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint seconePoint = [secondPointValue CGPointValue];
//把笔触移动一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
//笔触和另一个点形成一个路径
CGContextAddLineToPoint(context, seconePoint.x, seconePoint.y);
}
}
//真正绘制
CGContextStrokePath(context);
}
-(UIImage*)convertViewToImage:(UIView*)v{
CGSize s = v.bounds.size;
UIGraphicsBeginImageContext(s);
[v.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
RootView *view=[[RootView alloc]init];
view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
view.backgroundColor=[UIColor greenColor];
[self.view addSubview:view];