ios画画板优化

 1 @interface NJView ()
 2 
 3 @property (nonatomic, strong) NSMutableArray *paths;
 4 
 5 @end
 6 
 7 @implementation NJView
 8 
 9 - (NSMutableArray *)paths
10 {
11     if (_paths == nil) {
12         _paths = [NSMutableArray array];
13     }
14     return _paths;
15 }
16 
17 // 开始触摸
18 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
19 {
20 
21     // 1.获取手指对应UITouch对象
22     UITouch *touch = [touches anyObject];
23     // 2.通过UITouch对象获取手指触摸的位置
24     CGPoint startPoint = [touch locationInView:touch.view];
25     
26     // 3.当用户手指按下的时候创建一条路径
27     UIBezierPath *path = [UIBezierPath bezierPath];
28     // 3.1设置路径的相关属性
29     [path setLineJoinStyle:kCGLineJoinRound];
30     [path setLineCapStyle:kCGLineCapRound];
31     [path setLineWidth:10];
32 
33     
34     // 4.设置当前路径的起点
35     [path moveToPoint:startPoint];
36     // 5.将路径添加到数组中
37     [self.paths addObject:path];
38     
39 }
40 // 移动
41 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
42 {
43     // 1.获取手指对应UITouch对象
44     UITouch *touch = [touches anyObject];
45     // 2.通过UITouch对象获取手指触摸的位置
46     CGPoint movePoint = [touch locationInView:touch.view];
47     
48     // 3.取出当前的path
49     UIBezierPath *currentPaht = [self.paths lastObject];
50     // 4.设置当前路径的终点
51     [currentPaht addLineToPoint:movePoint];
52     
53     // 6.调用drawRect方法重回视图
54     [self setNeedsDisplay];
55 
56 }
57 
58 // 离开view(停止触摸)
59 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
60 {
61     
62     [self touchesMoved:touches withEvent:event];
63 
64 }
65 
66 // 画线
67 - (void)drawRect:(CGRect)rect
68 {
69     
70     [[UIColor redColor] set];
71     // 边路数组绘制所有的线段
72     for (UIBezierPath *path in self.paths) {
73         [path stroke];
74     }
75     
76 }
77 
78 - (void)clearView
79 {
80     [self.paths removeAllObjects];
81     [self setNeedsDisplay];
82 }
83 - (void)backView
84 {
85     [self.paths removeLastObject];
86     [self setNeedsDisplay];
87 }
88 
89 
90 @end

 

posted @ 2016-01-23 19:43  xiaocaoera  阅读(220)  评论(0编辑  收藏  举报