gpt4 book ai didi

ios - 在 UIView 上涂鸦 - ios 开发

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:39 25 4
gpt4 key购买 nike

你好,我正在做这个项目,允许用户在 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 呢?我还没有学到任何关于它的东西,所以我试图避免使用它。但如果这是必须的,我会试一试。

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

最佳答案

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

一般我的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();
}

而且有效!

附注我找到了

 PreviousPoint = [touch previousLocationInView:image];

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

关于ios - 在 UIView 上涂鸦 - ios 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com