iphone - UITapGestureRecognizer 识别为 UILongPressGestureRecognizer

标签 iphone ios objective-c uigesturerecognizer gesture

我的 Collection View 中的 UITapGestureRecognizer 有问题,我不知道错误。

我想在有长按手势时做一个自定义 Action ,而当有点击手势时我什么都不做,所以我有这些方法:

- (void)activateSelectionMode:(UILongPressGestureRecognizer *)gr
{
    if (![self.collectionView allowsSelection]) {
        [self.collectionView setAllowsSelection:YES];
        NSLog(@"Seleccion activada");
    }
}

- (void)pruebaTap:(UITapGestureRecognizer *)tr
{
    NSLog(@"tap");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:touchPoint];
    if (indexPath != nil && [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        CVCell *cell =  (CVCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

        if ([[cell checkImage] isHidden]) {
            // TODO: Añadir la celda a la lista de celdas seleccionadas
            [[cell checkImage] setHidden:NO];
            NSLog(@"Seleccionada celda %@", [[cell titleLabel] text]);
        } else {
            // TODO: Quitar la celda de la lista de celdas seleccionadas
            [[cell checkImage] setHidden:YES];
            NSLog(@"No seleccionada celda %@", [[cell titleLabel] text]);
        }

        NSLog(@"Entra");

        return YES;
    }

    return NO;
}

如果我注释最后一个方法,每个方法都被完美识别,但如果我不注释最后一个方法,点击手势被识别为长按手势。这里我将手势分配给 Collection View :

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pruebaTap:)];
tap.delegate = self;
[self.collectionView addGestureRecognizer:tap];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(activateSelectionMode:)];
longPress.delegate = self;
[self.collectionView addGestureRecognizer:longPress];

提前致谢。

最佳答案

不确定您是否实现了以下手势委托(delegate)方法。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer;

如果你还没有实现那么没问题,因为默认实现返回 NO 但如果你已经实现并返回 YES 那么这两个手势都会被识别。可能返回 NO 将解决您的问题

关于iphone - UITapGestureRecognizer 识别为 UILongPressGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531227/

相关文章:

ios - 滑动删除不适用于 TableVIewCell?

objective-c - 奇怪的错误 NSAssert

objective-c - 如何管理在辅助线程中运行的 NSRunLoop 的自动释放池?

iphone - UIButton 以编程方式创建但未显示在 View 中(调用了 addSubview)

ios - Game Center 身份验证在 iOS 8 中不起作用

iphone - 获取设备的宽度和高度

ios - 在 iOS CoreGraphics 中减小渲染 PDF 的文件大小

android - 当应用程序处于特定状态时,以编程方式限制用户访问 Android/iPad/iPhone 设备

ios - 通过 CocoaPods 使用 Google Analytics 时,“链接器命令失败,退出代码为 1”

ios - iOS 11 上搜索栏内的搜索文本字段位置错误