【问题标题】:UITapGestureRecognizer recognized as UILongPressGestureRecognizerUITapGestureRecognizer 被识别为 UILongPressGestureRecognizer
【发布时间】:2013-08-30 10:57:32
【问题描述】:

我的集合视图中的 UITapGestureRecognizer 存在问题,我不知道错误。

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

- (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;
}

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

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];

非常感谢。

【问题讨论】:

  • "如果我注释了最后一个方法,每个方法都被完美识别,但是如果我不注释最后一个方法"--你说的最后一个方法是什么意思?

标签: iphone ios objective-c uigesturerecognizer gesture


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    它肯定会识别长按手势,因为,你最后添加了它,你正在做的是,你在同一个视图上添加 2 个手势,所以这里你的 longPress 手势将在 UITapGestureRecognizer 手势(即点击)上重叠,所以每次长按手势都会被调用。

    你可以做的是,你必须一次添加一个。

    【讨论】:

    • 我有同样的问题,但没有添加水龙头识别器:/
    • 你到底想说什么?你没有添加点击手势,你只添加了UILongpress手势和长按方法(activateSelectionMode)没有调用???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2012-12-10
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多