UITapGestureRecognizer被识别为UILongPressGestureRecognizer

时间:2013-08-30 10:57:33

标签: iphone ios objective-c uigesturerecognizer gesture

我的集合视图中的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];

非常感谢。

2 个答案:

答案 0 :(得分:1)

不确定天气是否已实施以下手势委托方法。

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

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

答案 1 :(得分:0)

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

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