I have an issue with the UITapGestureRecognizer in my collection view and I don't know the error.
I want to do a custom action when there is a long press gesture, and when there is a tap gesture I want to do nothing, so I have these methods:
- (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;
}
IF I comment the last method, each method is recognized perfectly, but if I don't comment the last method, the tap gesture is recognized as long press gesture. Here I assign the gesture to the 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];
Thanks so much in advance.
Not sure weather you have implemented the below gesture delegate method or not.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer;
If you have not implemented then no problem because default implementation returns NO but if you have implemented & returned YES then both the gesture will be recognized. May be returning NO will resolve your problem
It will definitely recognize long press gesture because , you have added it last, what you are doing is , you are adding 2 gestures on same view, so here your longPress gesture will overlap on UITapGestureRecognizer gesture(that is tap), so everytime longpress gesture will be called.
what you can do is , you will have to add one at a time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.