shouldReceiveTouch未调用

时间:2013-04-11 11:31:39

标签: ios uigesturerecognizer

我在网上看到了很多关于它的问题,特别是在StackOverflow上。 我测试了许多给出的答案但在我的情况下,没有任何作用。

我的类实现了协议UIGestureRecognizerDelegate

@interface CDMapViewController : CDViewController <UIGestureRecognizerDelegate>

以下方法是在我的类的@implentation

中从xcode自动完成中编写的
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"not called");
    return NO;
}

我已经在第一种方法中正确启动了UIGestureRecognizer,正确调用了第二种,第三种和第四种方法:

- (void)initGestureOnMap {
    UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] init];
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
    [super touchesBegan:touches withEvent:event];
    gesture_dragging = NO;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    gesture_dragging = YES;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if (gesture_dragging || [touches count] != 1) return;
        /* bla bla bla */
}

......它没有记录 - 没有被称为......为什么?

1 个答案:

答案 0 :(得分:2)

您需要为super方法调用touches实现。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
    [super touchesBegan:touches withEvent:event];
    gesture_dragging = NO;
}

... and so on.

这些方法需要在视图上实现,而不是在视图控制器上实现。

选择你想要的手势。 UIGestureRecognizer本身并没有做多少,所以请选择UITapGestureRecognizer之类的。{1}}。接下来,使用指定的初始化程序实现手势识别器。

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethod:)];

最后,实施myMethod:

-(void)myMethod:(UITapGestureRecognizer *)recognizer
{
    // Whatever this does.
}