画图-之涂鸦
- 画图-之涂鸦 推荐度:
- 相关推荐
画图-之涂鸦
先上代码吧:
-(NSMutableArray *)totalPathPoints{if (_totalPathPoints == nil) {_totalPathPoints = [NSMutableArray array];}return _totalPathPoints;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {CGContextRef ctx = UIGraphicsGetCurrentContext();for (NSMutableArray *pathPoints in self.totalPathPoints) {for (int i = 0 ; i < pathPoints.count; i ++) {CGPoint pos = [pathPoints [i]CGPointValue];if (i == 0) {CGContextMoveToPoint(ctx, pos.x, pos.y);}else{CGContextAddLineToPoint(ctx, pos.x, pos.y);}}}CGContextSetLineCap(ctx, kCGLineCapRound);CGContextSetLineJoin(ctx, kCGLineJoinRound);CGContextSetLineWidth(ctx, 5);CGContextStrokePath(ctx);}/**
* 起点
*
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{UITouch *touch = [touches anyObject];CGPoint startPos = [touch locationInView:touch.view] ;// 每一次开始触摸, 就新建一个数组来存放这次触摸过程的所有点(这次触摸过程的路径)NSMutableArray *pathPoints = [NSMutableArray array];[pathPoints addObject:[NSValue valueWithCGPoint:startPos]];// 添加这次路径的所有点到大数组中[self.totalPathPoints addObject:pathPoints];[self setNeedsDisplay];}
/*** 连线**/
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{UITouch *touch = [touches anyObject];CGPoint pos = [touch locationInView:touch.view];// 取出这次路径对应的数组NSMutableArray *pathPoints = [self.totalPathPoints lastObject];[pathPoints addObject:[NSValue valueWithCGPoint:pos]];[self setNeedsDisplay];}
/*** 连线**/
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{[self touchesMoved:touches withEvent:event];
}
图片如下:
希望可以帮助到你,有问题请关注我哦,我们一起coding.
最新文章
- iOS annotation
- 【时间复杂度】时间复杂度
- GridView,GridLayout
- 程序与软件的易用性
- 职场杂谈
- IIS攻击与日志
- TextOut字体设置
- [SOA介绍]什么是SOA?
- anchor的用法
- ext2和ext3的主要区别
- ExtJS (3.3的使用)
- HashTable 使用
- 卷积到底卷了个啥?
- Windows上CLion配置和使用教程
- [机器学习算法]支持向量机SVM原理简介
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron的作用和不同
- 网页弹出对话框的几种代码
- Linux命令curl详解(一)
- R reticulate 设置 python 环境
- 1637