0
我正在研究一个照片编辑应用程序,并且需要用户能够绘制出裁剪照片的路径。我有一个类与UIView一起工作,用于绘制流畅的UIBezierPath行。不过,我真的需要将它应用于UIImageView,以便可以在图像上完成绘图。如果我将该类更改为UIImageView的子类,则不再有效。任何想法,我可以做些什么来解决这个问题?或者更好的选择来实现相同的目标?下面是我的实现:在UIImageView上为图像裁剪画UIBezierPath
#import "DrawView.h"
@implementation DrawView
{
UIBezierPath *path;
}
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO]; // (2)
[self setBackgroundColor:[UIColor whiteColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void)drawRect:(CGRect)rect // (5)
{
[[UIColor blackColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
@end
如果我把上的touchesBegan一个破发点或者touchesMoved预期当它火。
好吧,我也有和无userInteractionEnabled试了一下,就像我说的,即使没有它,我仍然得到的touchesBegan火。不过,您的其他解决方案可能是我需要的。你能否更深入地解释你的解决方案?谢谢您的回复! –
这是行不通的,但是由于对[self setNeedsDisplay]的调用,它运行速度很慢;在后台重新绘制图像。没有刷新图像刷新绘图吗? –
作为替代解决方案,您可以尝试将DrawView放在Interface Builder中的UIImageView上。在这种情况下,您不应该在DrawView中绘制图像。然后,UIImageView将绘制图像,而DrawView将只绘制更加卑鄙的路径。此外,您可以使用 - [self setNeedsDisplayInRect:path.bounds]来最小化要重绘的区域。 –