在UIView上涂鸦 - ios开发

时间:2012-03-28 19:40:36

标签: ios uiview touch quartz-graphics

您好我正在处理此项目以允许用户在UIView上进行涂鸦。我的方法是创建一个CGMutablePathRef路径,并在TouchMoved时为其添加新行。

下面列出了相关的代码,这些代码属于UIView类。

static CGMutablePathRef path; //create it as static value. 
//I got this habit from java coding but actually not sure 
//if it's a good way to do it in objective-c.


//draw the path

-(void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
    CGPathRelease(path);
}

//touch began. create the path and add point.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    path = CGPathCreateMutable();
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGPathMoveToPoint(path, NULL, point.x, point.y);
    [self setNeedsDisplay];            
}


//add points when touch moved
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    CGPathAddLineToPoint(path, NULL, point.x, point.y);
    [self setNeedsDisplay];

}

//last points when touch ends
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGPathAddLineToPoint(path, NULL, point.x, point.y);
    [self setNeedsDisplay];
}

然而,我遇到了错误,我并没有真正理解它们......我想这与UIGestureRecognizer有关,我从其他涂鸦代码中看到了这一点。是否有必要添加UIGestureRecognizer?我还没有学到任何东西,所以我试图避免使用它。但如果这是必须的,我会试一试。

我想到的另一种方法是将所有点位置存储到一个可变数组中,因为我必须将这些点位置值存储在某处。但是,我不知道数组是否合适,因为我找不到将浮点值存储到数组的方法。如果有人可以帮助我这将是非常有帮助的。谢谢!

1 个答案:

答案 0 :(得分:1)

感谢那些向我提供帮助的人。通过一堆代码和方法看后,幸运的是我找到了解决这个问题的有效方法!

通常,我绘制时,SketchView中的imageView会动态更新。也就是说,每次我绘制更多像素时,都会在该新像素上添加一行,更改UIImageView,然后将其设置为UIView的背景图像。

SketchView.h

@property (assign, nonatomic) CGPoint CurrentPoint;
@property (assign, nonatomic) CGPoint PreviousPoint;
@property (assign, nonatomic) CGPoint InitialPoint;

SketchView.m

@synthesize CurrentPoint;
@synthesize PreviousPoint;
@synthesize InitialPoint;


//begin the touch. store the initial point because I want to connect it to the last   
//touch point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:image];
    InitialPoint = point;

    }


//When touch is moving, draw the image dynamically
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];
PreviousPoint = [touch previousLocationInView:image];
CurrentPoint = [touch locationInView:image];
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext();   
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width,   image.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y);
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y);
CGContextStrokePath(ctx);
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];
PreviousPoint = [touch previousLocationInView:image];
CurrentPoint = [touch locationInView:image];
UIGraphicsBeginImageContext(image.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext();
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y);
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y);

//I connected the last point to initial point to make a closed region
CGContextMoveToPoint(ctx, CurrentPoint.x, CurrentPoint.y);
CGContextAddLineToPoint(ctx, InitialPoint.x, InitialPoint.y);
CGContextStrokePath(ctx);
image.image = UIGraphicsGetImageFromCurrentImageContext();


UIGraphicsEndImageContext();
}

它有效!

P.S。我找到了

 PreviousPoint = [touch previousLocationInView:image];

非常有帮助,虽然在我发现涂鸦的代码中没有提到太多......我希望这会有所帮助。 :)