@property (nonatomic, retain)NSMutableArray *allLinesMutableArray;//用来存储所有线条的数组
@property (nonatomic, retain)NSMutableArray *colorMutableArray;//用来存储所有颜色的数组
@property (nonatomic, retain)UIButton *eraserButton;//清除橡皮擦
@property (nonatomic, retain)UIButton *allEraseButton;//清除所有的线条的橡皮擦
@end
@implementation PaintView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
_eraserButton = [UIButton buttonWithType:UIButtonTypeSystem];
_eraserButton.frame = CGRectMake(50, 10, 100, 80);
[_eraserButton setTitle:@"橡皮擦" forState:UIControlStateNormal];
[_eraserButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_eraserButton];
_allEraseButton = [UIButton buttonWithType:UIButtonTypeSystem];
_allEraseButton.frame = CGRectMake(170, 10, 100, 80);
[_allEraseButton setTitle:@"清除所有" forState:UIControlStateNormal];
[_allEraseButton addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_allEraseButton];
}
return self;
}
//画笔的实现方法 - (void)btnAction: (UIButton *)sender{
if (self.allLinesMutableArray.count) {
[self.allLinesMutableArray removeLastObject];
[self setNeedsDisplay];
}
}
//清除所有线条按钮的实现方法 - (void)btn{
if (self.allLinesMutableArray.count) {
[self.allLinesMutableArray removeAllObjects];
[self setNeedsDisplay];
}
}
//线条数组的懒加载
- (NSMutableArray *)allLinesMutableArray{
if (!_allLinesMutableArray) {
_allLinesMutableArray = [[NSMutableArray alloc]initWithCapacity:2];
}
return _allLinesMutableArray;
}
//颜色数组的懒加载 - (NSMutableArray *)colorMutableArray{
if (!_colorMutableArray) {
_colorMutableArray = [[NSMutableArray alloc]initWithCapacity:2];
}
return _colorMutableArray;
}
//触摸开始 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//得到绘图的起始点
//得到触摸东西
UITouch *touch = [touches anyObject];
// 得到起始点
CGPoint startPoint = [touch locationInView:self.superview];
// 将起始点存储到一条线中
// 先初始化一条贝塞尔曲线
UIBezierPath *bezierLine = [UIBezierPath bezierPath];
// 将起始点存储到线里面
[bezierLine moveToPoint:startPoint];
// 将贝塞尔曲线存储到数组中,这块必须使用.语法的形势,来给数组赋值
[self.allLinesMutableArray addObject:bezierLine];
// 每新增一条线,就给线条添加对应的颜色
[self.colorMutableArray addObject:RGBA];
}
//触摸开始移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 手指触摸移动的时候,才会产生新的点,将这些所有的点都存储到贝塞尔曲线中,才能真正的组成一条线
UITouch *touch = [touches anyObject];
// 得到当前你在父视图上的点
CGPoint locatiPoint = [touch locationInView:self.superview];
// 取出开始触摸的方法中初始化好的贝塞尔曲线,因为刚才
UIBezierPath *bezierLine = self.allLinesMutableArray.lastObject;
// 将得到的点放到贝塞尔线中
[bezierLine addLineToPoint:locatiPoint];
// 重新绘制当前界面
[self setNeedsDisplay];//调用此方法,系统会触发drawRect方法,重新绘制当前界面
}
// 重新绘制当前界面
-
(void)drawRect:(CGRect)rect{
// 设置画笔,如果是设置一种颜色的话,就用这种方法,如果想多种颜色共存的话,就是下面的方法
// [ RGBA setStroke];
// 开始画线
// 遍历所有的线条。开始绘制
for (UIBezierPath *line in self.allLinesMutableArray) {UIColor *linColor = [self .colorMutableArray objectAtIndex:[self.allLinesMutableArray indexOfObject:line]];
// 设置每条线的画笔
[linColor setStroke];
// 设置线条粗细
[line setLineWidth:30];
// 开始绘制
[line stroke];
}
}
//触摸被打断
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
//触摸被打断
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self setNeedsDisplay];
}