ios - shouldReceiveTouch 未调用

标签 ios uigesturerecognizer

我在网络上看到很多关于它的问题,特别是在 StackOverflow 上。 我测试了许多给定的答案,但就我而言,没有任何效果。

我的类实现了协议(protocol)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 */
}

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

最佳答案

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

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

... and so on.

这些方法需要在您的 View 上实现,而不是在您的 View Controller 上。

选择您想要的手势类型。 UIGestureRecognizer 本身并没有做太多事情,所以选择一个像 UITapGestureRecognizer 这样的。接下来,使用指定的初始值设定项实现手势识别器。

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

最后,实现myMethod:

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

关于ios - shouldReceiveTouch 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947676/

相关文章:

ios - 使用键盘和中心自动布局管理 View

ios - 反转UITable行号从大到小(...5,4,3,2,1)

objective-c - 在 Storyboard上设置 reuseIdentifier 会导致自定义 UITableViewCell 不显示

ios - 为 iOS 7 编译 x264

ios - LongPressGestureRecognizer 从 View 背后识别

ios - UIButtons 上的 UISwipeGestureRecognizer

iphone - 跟踪 UIButton 上的手指触摸并执行它的操作

iphone - 试图从网站获取文本并在 ios 中显示?

ios - 如何在 Swift3 IOS 饼图中添加点击手势识别?

ios - 在 ScrollView 中的 ImageView 上使用 GestureRecognizer