简单的实现涂鸦功能

  1. - (id)initWithFrame:(CGRect)frame  
  2. {  
  3.     self = [super initWithFrame:frame];  
  4.     if (self) {  
  5.           
  6.         // Initialization code  
  7.         // 定义了一个线数组,来存储所有的线  
  8.         self.lineArray = [[NSMutableArray alloc] initWithCapacity:1];  
  9.         // 设置一个清除按钮  
  10.         UIButton *clear = [UIButton buttonWithType:UIButtonTypeSystem];  
  11.         clear.frame = CGRectMake(frame.size.width-100, frame.size.height-100100100);  
  12.         [clear setTitle:@"清除" forState:UIControlStateNormal];  
  13.         [clear addTarget:self action:@selector(clearScreen) forControlEvents:UIControlEventTouchUpInside];  
  14.         [self addSubview:clear];  
  15.           
  16.     }  
  17.     return self;  
  18. }  
  19.   
  20. - (void)clearScreen{  
  21.       
  22.     // 清除所有的线,即存在线数组中的对象  
  23.     [_lineArray removeAllObjects];  
  24.     // 重绘(具体概念在动画中更明显,这里理解为,接受到了命令,接下来就是点击开始按钮运行)  
  25.     [self setNeedsDisplay];  
  26.   
  27. }  
  28.   
  29.   
  30. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
  31.   
  32.     // 一触摸屏幕,就将启动点存进pointArray中,并且将绘成第一条线所需的点数组作为元素存进定义的_lineArray中  
  33.     // 为什么这样设置呢?因为手一离开屏幕,又是一个新的启动点传入,也是另一条新线的划入  
  34.     NSMutableArray * pointArray = [NSMutableArray arrayWithCapacity:1];  
  35.     [_lineArray addObject:pointArray];  
  36.       
  37. }  
  38.   
  39. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{  
  40.   
  41.     // 获得触摸屏幕的所有对象  
  42.     UITouch * touch = [touches anyObject];  
  43.     // 获得触摸屏幕的所有点的位置  
  44.     CGPoint  point = [touch locationInView:self];  
  45.     // 利用NSValue将获得的点封装成NSValue类型对象,以便能存进NSMutableArray中  
  46.     NSValue * pointCount = [NSValue valueWithCGPoint:point];  
  47.     // 与touchesBegan方法想呼应,一触摸屏幕所设置的点数组也就是最后一个点数组,这里取出来,存移动中所有点  
  48.     NSMutableArray * pointArray = [_lineArray lastObject];  
  49.     [pointArray addObject:pointCount];  
  50.       
  51.     // 重新绘制这个self.view  
  52.     [self setNeedsDisplay];  
  53.       
  54. }  
  55.   
  56.   
  57. // Only override drawRect: if you perform custom drawing.  
  58. // An empty implementation adversely affects performance during animation.  
  59. - (void)drawRect:(CGRect)rect  
  60. {  
  61.     // 获得上下文本(我更愿意理解为获得进行绘画资格,作为一个通行证般的参数,让你能设置画笔颜色,线条尺寸,以及如何绘画)  
  62.     CGContextRef context = UIGraphicsGetCurrentContext();  
  63.     // 设置画笔  
  64.     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);  
  65.     // 设置线条粗细  
  66.     CGContextSetLineWidth(context, 2.0);  
  67.       
  68.     for (int i = 0; i < _lineArray.count; i++) {  
  69.         NSMutableArray * pointArray = [_lineArray objectAtIndex:i];  
  70.         // 这里(int)pointArray.count进行了类型转换,可以思考下是为什么  
  71.         for (int j = 0; j < (int)pointArray.count - 1; j++) {  
  72.             // 获得封装的起点  
  73.             NSValue * headPointValue = [pointArray objectAtIndex:j];  
  74.             // 获得封装的终点  
  75.             NSValue * tailPointValue = [pointArray objectAtIndex:(j + 1)];  
  76.             // 转化为CGPoint类型  
  77.             CGPoint headPoint = [headPointValue CGPointValue];  
  78.             // 转化为CGPoint类型  
  79.             CGPoint tailPoint = [tailPointValue CGPointValue];  
  80.               
  81.             // 设置画笔的起点  
  82.             CGContextMoveToPoint(context, headPoint.x, headPoint.y);  
  83.             // 画笔路径的途径直到终点  
  84.             CGContextAddLineToPoint(context, tailPoint.x, tailPoint.y);  
  85.         }  
  86.     }  
  87.     // 绘制这个文本(总算可以画画了)  
  88.     CGContextStrokePath(context);  
  89.       
  90.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值