1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| // // PaintView.m // 画板-2 // // Created by zxc on 2021/4/5. //
#import "PaintView.h"
@interface PaintView()
@property (nonatomic,strong) UIBezierPath *path; @property (nonatomic,strong) NSMutableArray *paths; @end
@implementation PaintView
-(NSMutableArray *)paths { if (_paths==nil) { _paths=[NSMutableArray array]; } return _paths; }
//确定起点 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //创建路径 UIBezierPath *path=[UIBezierPath bezierPath]; _path=path; [self.paths addObject:path]; //获取触摸点 CGPoint pos=[self pointWithTouches:touches]; //确定起点 [path moveToPoint:pos]; }
-(CGPoint)pointWithTouches:(NSSet *)touches { UITouch *touch=[touches anyObject]; return [touch locationInView:self]; } //确定终点 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //获取触摸点 CGPoint pos=[self pointWithTouches:touches]; [_path addLineToPoint:pos]; //重绘 [self setNeedsDisplay]; }
// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code for (UIBezierPath *path in self.paths) { [path stroke]; } }
@end
|