画图-之涂鸦

先上代码吧:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值