码迷,mamicode.com
首页 > 移动开发 > 详细

IOS-涂鸦

时间:2016-02-24 17:19:19      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

  1 //
  2 //  PaintView.m
  3 //  IOS_0224_涂鸦
  4 //
  5 //  Created by ma c on 16/2/24.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "PaintView.h"
 10 
 11 @interface PaintView ()
 12 
 13 //存放每次触摸的完整路径
 14 @property (nonatomic, strong) NSMutableArray *path;
 15 
 16 @end
 17 
 18 @implementation PaintView
 19 
 20 - (NSMutableArray *)path
 21 {
 22     if (_path == nil) {
 23         _path = [NSMutableArray array];
 24     }
 25     return _path;
 26 }
 27 
 28 /*方法一*/
 29 /*
 30 //确定起点
 31 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 32 {
 33     UITouch *touch = [touches anyObject];
 34     CGPoint startPoint = [touch locationInView:touch.view];
 35     
 36     //每次开始触摸,就新建一个数组来存放这次触摸的所有点
 37     NSMutableArray *pathPoints = [NSMutableArray array];
 38     [pathPoints addObject:[NSValue valueWithCGPoint:startPoint]];
 39     //添加每次触摸的所有点形成的路径到path中
 40     [self.path addObject:pathPoints];
 41     
 42     [self setNeedsDisplay];
 43 }
 44 //连线
 45 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 46 {
 47     UITouch *touch = [touches anyObject];
 48     CGPoint currentPoint = [touch locationInView:touch.view];
 49     
 50     //取出这次路径对应的数组
 51     NSMutableArray *pathPoints = [self.path lastObject];
 52     [pathPoints addObject:[NSValue valueWithCGPoint:currentPoint]];
 53     
 54     [self setNeedsDisplay];
 55 
 56 }
 57 //结束点
 58 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 59 {
 60     UITouch *touch = [touches anyObject];
 61     CGPoint endPoint = [touch locationInView:touch.view];
 62     
 63     //取出这次路径对应的数组
 64     NSMutableArray *pathPoints = [self.path lastObject];
 65     [pathPoints addObject:[NSValue valueWithCGPoint:endPoint]];
 66 
 67     [self setNeedsDisplay];
 68 
 69 }
 70 
 71 - (void)drawRect:(CGRect)rect {
 72     CGContextRef ctx = UIGraphicsGetCurrentContext();
 73     
 74     for (NSMutableArray *points in self.path) {
 75         for (int i = 0; i < points.count; i++) { //一条路径
 76             
 77             CGPoint point = [points[i] CGPointValue];
 78             
 79             if (i == 0) {
 80                 CGContextMoveToPoint(ctx, point.x, point.y);
 81             }else{
 82                 CGContextAddLineToPoint(ctx, point.x, point.y);
 83             }
 84         }
 85     }
 86     
 87     CGContextSetLineCap(ctx, kCGLineCapRound);
 88     CGContextSetLineJoin(ctx, kCGLineJoinRound);
 89     CGContextSetLineWidth(ctx, 5);
 90     CGContextStrokePath(ctx);
 91 }
 92 
 93  */
 94 
 95 /*方法二*/
 96 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 97 {
 98     //1.获得当前触摸点
 99     UITouch *touch = [touches anyObject];
100     CGPoint startPoint = [touch locationInView:touch.view];
101     //2.创建一个新的路径
102     UIBezierPath *startPath = [UIBezierPath bezierPath];
103     
104     startPath.lineWidth = 5;
105     startPath.lineCapStyle = kCGLineCapRound;
106     startPath.lineJoinStyle = kCGLineJoinRound;
107     //设置起点
108     [startPath moveToPoint:startPoint];
109     //3.添加路径到数组中
110     [self.path addObject:startPath];
111     
112     [self setNeedsDisplay];
113     
114 }
115 
116 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
117 {
118     UITouch *touch = [touches anyObject];
119     CGPoint currentPoint = [touch locationInView:touch.view];
120     
121     UIBezierPath *currentPath = [self.path lastObject];
122     [currentPath addLineToPoint:currentPoint];
123     
124     [self setNeedsDisplay];
125 }
126 
127 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
128 {
129     [self touchesMoved:touches withEvent:event];
130     
131 }
132 
133 - (void)drawRect:(CGRect)rect
134 {
135     [[UIColor redColor] set];
136     for (UIBezierPath *path in self.path) {
137         [path stroke];
138     }
139 }
140 
141 - (void)clear
142 {
143     [self.path removeAllObjects];
144     [self setNeedsDisplay];
145 }
146 
147 - (void)back
148 {
149     [self.path removeLastObject];
150     [self setNeedsDisplay];
151 }
152 
153 @end

 

IOS-涂鸦

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5213465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!