UIView上的涂鸦-ios开发

UIView上的涂鸦-ios开发,ios,uiview,touch,quartz-graphics,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 obj

您好,我在这个项目上工作,允许用户涂鸦的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];
}
然而,我犯了一些错误,我并没有真正理解它们。。。我猜这是UIgestureRecognitor的问题,我从其他涂鸦代码中看到了这一点。是否需要将UIgestureRecognitor添加到其中?我对它一无所知,所以我尽量避免使用它。但如果必须的话,我会试试看


我正在考虑的另一种方法是将所有点位置存储到可变数组中,因为我必须将这些点位置值存储在某个位置。然而,我不知道数组是否合适,因为我还没有找到将浮点值存储到数组的方法。如果有人能帮我做这件事,那将非常有帮助。谢谢大家!

感谢那些给我提供帮助的人。在浏览了一系列代码和方法之后,幸运的是我找到了解决这个问题的有效方法

通常,在绘图时,SketchView中的imageView会动态更新。也就是说,每次我画更多的像素,一条线将添加到新的像素,更改UIImageView,然后将其设置为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];
}
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、 我在美国找到的

 PreviousPoint = [touch previousLocationInView:image];

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

没有必要使用UIGestureRecognitors。至于将浮点数存储到NSMutableArray,请查看numberWithFloat:]。再看看这个问题:我建议使用手势识别器。它使您的代码更简单,更易于扩展。@rokjarc是的,链接的问题非常有用。还有一个小问题:该答案中使用的类是UIImageView,因此在它调用的方法中[self.image drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)]。但在我的例子中,它是UIView,那么我应该调用什么方法来绘制呢?(对不起,我想这是个愚蠢的问题。我真的是个新手。)谢谢!