2

我在网上看到了很多关于它的问题,尤其是在 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 */
}

...它不记录-不调用...为什么?

4

1 回答 1

2

您需要调用方法super的实现touches

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

... and so on.

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

选择你想要的手势。就其本身而言,UIGestureRecognizer并没有多大作用,所以选择一个喜欢UITapGestureRecognizer. 接下来,使用指定的初始化程序实现您的手势识别器。

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

最后,实施myMethod:

-(void)myMethod:(UITapGestureRecognizer *)recognizer
{
    // Whatever this does.
}
于 2013-04-11T11:34:37.010 回答