画图-之涂鸦

时间: 2023-08-02 admin IT培训

画图-之涂鸦

画图-之涂鸦

先上代码吧:

-(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.