我的 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/