@property (nonatomic, retain)NSMutableArray *allLinesMutableArray;//用來存儲所有線條的數(shù)組
@property (nonatomic, retain)NSMutableArray *colorMutableArray;//用來存儲所有顏色的數(shù)組
@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;
}
//畫筆的實現(xiàn)方法 - (void)btnAction: (UIButton *)sender{
if (self.allLinesMutableArray.count) {
[self.allLinesMutableArray removeLastObject];
[self setNeedsDisplay];
}
}
//清除所有線條按鈕的實現(xiàn)方法 - (void)btn{
if (self.allLinesMutableArray.count) {
[self.allLinesMutableArray removeAllObjects];
[self setNeedsDisplay];
}
}
//線條數(shù)組的懶加載
- (NSMutableArray *)allLinesMutableArray{
if (!_allLinesMutableArray) {
_allLinesMutableArray = [[NSMutableArray alloc]initWithCapacity:2]脓豪;
}
return _allLinesMutableArray;
}
//顏色數(shù)組的懶加載 - (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];
// 將貝塞爾曲線存儲到數(shù)組中,這塊必須使用.語法的形勢扫夜,來給數(shù)組賦值
[self.allLinesMutableArray addObject:bezierLine];
// 每新增一條線历谍,就給線條添加對應(yīng)的顏色
[self.colorMutableArray addObject:RGBA];
}
//觸摸開始移動 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 手指觸摸移動的時候,才會產(chǎn)生新的點印蔬,將這些所有的點都存儲到貝塞爾曲線中脱衙,才能真正的組成一條線
UITouch *touch = [touches anyObject];
// 得到當(dāng)前你在父視圖上的點
CGPoint locatiPoint = [touch locationInView:self.superview];
// 取出開始觸摸的方法中初始化好的貝塞爾曲線,因為剛才
UIBezierPath *bezierLine = self.allLinesMutableArray.lastObject;
// 將得到的點放到貝塞爾線中
[bezierLine addLineToPoint:locatiPoint];
// 重新繪制當(dāng)前界面
[self setNeedsDisplay];//調(diào)用此方法,系統(tǒng)會觸發(fā)drawRect方法捐韩,重新繪制當(dāng)前界面
}
// 重新繪制當(dāng)前界面
-
(void)drawRect:(CGRect)rect{
// 設(shè)置畫筆退唠,如果是設(shè)置一種顏色的話,就用這種方法荤胁,如果想多種顏色共存的話瞧预,就是下面的方法
// [ RGBA setStroke];
// 開始畫線
// 遍歷所有的線條。開始繪制
for (UIBezierPath *line in self.allLinesMutableArray) {UIColor *linColor = [self .colorMutableArray objectAtIndex:[self.allLinesMutableArray indexOfObject:line]];
// 設(shè)置每條線的畫筆
[linColor setStroke];
// 設(shè)置線條粗細
[line setLineWidth:30];
// 開始繪制
[line stroke];
}
}
//觸摸被打斷
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
//觸摸被打斷
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self setNeedsDisplay];
}