繁体   English   中英

UITapGestureRecognizer被识别为UILongPressGestureRecognizer

[英]UITapGestureRecognizer recognized as UILongPressGestureRecognizer

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

非常感谢提前。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM