【发布时间】: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