为什么 touchesbegan:在使用 UIPinchGestureRecognizer 后永远不会在 UIView 上被调用?

Why touchesbegan: never gets called on a UIView after UIPinchGestureRecognizer is used?

我正在开发一个绘图应用程序,它允许用户在由它的 superView (self.view) 子视图的 UIView (InsideView) 上绘制 UIbezierpath。我在 InsideView 中使用传统的绘图代码,例如 touchesBegan:, touchesMoved:, touchesEnded:,我在 viewController class(这是 InsideView 的超级视图)中处理缩放代码 handlePinch:(UIPinchGestureRecognizer *)recognizer:

当我先绘制时,我可以在它之后进行缩放,但是当我先进行缩放时,绘图代码(touchesBegan 等)在 UIPinchGestureRecognizer 被触发后不会被调用并且它不会在 InsideView 上绘制任何东西.我究竟做错了什么?提前致谢。

//InsideView中的代码(它是一个UIView子class并且是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;
}

我开发了一个 UIViewController,我可以毫无问题地将它们一起使用,也许解决方案是将此逻辑从 UIView 移动到 UIViewController。所以在 viewDidLoad 你可以像这样定义捏合识别器

UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.view addGestureRecognizer:pinch];

当然也应该将方法 handlePinch:touchesBegan:withEvent: 一起移至 UIViewControllertouchesMoved:withEvent:touchesEnded:withEvent: 方法

这些方法中你应该改变的是你应该改变

self

self.yourSpecificCurrentUIView