我正在开发一个绘图应用程序,它允许用户在它的superView(self.view)子视图的UIView(InsideView)上绘制UIbezier路径。我在InsideView中使用常规绘图代码,如touchesBegan:, touchesMoved:, touchesEnded:
,我在viewController类(InsideView的superView)中处理缩放缩放代码handlePinch:(UIPinchGestureRecognizer *)recognizer:
。
当我先画画时,我可以在它之后进行缩放变焦,但是当我首先进行缩放变焦时,绘制代码(touchesBegan等)在UIPinchGestureRecognizer被触发后不会被调用,并且它不会在InsideView上绘制任何内容。我究竟做错了什么?提前致谢。
// InsideView中的代码(它是一个UIView子类,是self.view的子视图)
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
path = [UIBezierPath bezierPath];
[path setLineWidth:7.0];
pointsArray = [NSMutableArray array];
finish = NO;
}
return self;
}
-(void)drawRect:(CGRect)rect{
[[UIColor redColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
[pointsArray addObject:[NSValue valueWithCGPoint:p]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
[pointsArray addObject:[NSValue valueWithCGPoint:p]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[path closePath];
//Smoothing the path.
path.miterLimit=-10;
[self setNeedsDisplay];
finish = YES;
}
//Code in the viewController (InsideView's superView)
- (void)viewDidLoad {
[super viewDidLoad];
InsideView* sV = [InsideView new];
[sV setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:sV];
//Pinch
UIPinchGestureRecognizer*pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
pinch.delegate =self;
[sV addGestureRecognizer:pinch];
}
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{
if ([recognizer numberOfTouches] < 2)
return;
if (recognizer.state == UIGestureRecognizerStateBegan) {
lastScale = 1.0;
lastPoint = [recognizer locationInView:self.view];
}
// Scale
CGFloat scale = 1.0 - (lastScale - recognizer.scale);
[sV.layer setAffineTransform:
CGAffineTransformScale([sV.layer affineTransform],
scale,
scale)];
lastScale = recognizer.scale;
// Translate
CGPoint point = [recognizer locationInView:self.view];
[sV.layer setAffineTransform:
CGAffineTransformTranslate([sV.layer affineTransform],
point.x - lastPoint.x,
point.y - lastPoint.y)];
lastPoint = [recognizer locationInView:self.view];
}
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
sV.transform = CGAffineTransformScale(sV.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1.0;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
答案 0 :(得分:2)
我开发了一个UIViewController,我一起使用它们没有任何问题,或许解决方案可能是将这个逻辑从UIView移动到UIViewController。所以在viewDidLoad
你可以像这样定义捏合识别器
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.view addGestureRecognizer:pinch];
当然,方法handlePinch:
也应与UIViewController
一起移至touchesBegan:withEvent:
,
touchesMoved:withEvent:
,touchesEnded:withEvent:
方法
这些方法应该改变的是你应该改变
self
到
self.yourSpecificCurrentUIView