码迷,mamicode.com
首页 > 其他好文 > 详细

画图-之涂鸦

时间:2015-07-22 10:54:41      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

先上代码吧:

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

画图-之涂鸦

标签:

原文地址:http://blog.csdn.net/tubiebutu/article/details/46997591

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