【发布时间】:2015-12-09 05:45:15
【问题描述】:
我正在开发一个照片编辑应用程序,并且需要用户能够绘制一条路径来裁剪照片。我有一个与 UIView 一起使用的类,用于绘制平滑的 UIBezierPath 线。但是我真的需要将它应用于 UIImageView 以便可以在图像上完成绘图。如果我将类更改为 UIImageView 的子类,则它不再起作用。关于我能做些什么来解决这个问题的任何想法?还是实现相同目标的更好选择?下面是我的实现:
#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 上放置一个断点,它会按预期触发。
【问题讨论】:
标签: ios objective-c uiimageview uibezierpath