我的集合视图中的 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];
提前非常感谢。