1

When I add a UILongPressGestureRecognizer on my collectionView (I'm doing it like in this answer), the collection view stops highlighting cells as expected.

I'm handling the cell highlighting in my UICollectionViewCellSubclass, like it's explained in this answer:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (self.highlighted) {
        //Here I show my highlighting layer
    }
    else {
        //Here I hide my highlighting layer   
    }
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    [self setNeedsDisplay]; //This call draw rect and show/hide the highlighting layer
}

The problem seems to be that, when I add the UILongPressGestureRecognizer gesture on the collection view, drawRect is not being called after setting highlighted to YES.

If I move the logic to show my highlighting layer in setHighlighted:YES, I can't see the touching-feedback because setHighlighted:NO its being called immediately after! (This happens only after adding the UILongPressGestureRecognizer).

I've also tried to move the logic in touchesBegan/touchesEnded but then didSelectRowAtIndexPath: doesn't work anymore.

Community
  • 1
  • 1
andreacipriani
  • 2,519
  • 26
  • 23

1 Answers1

0

Unfortunately, it looks like the UILongPressGestureRecognizer is blocking the touches required for the UICollectionView to highlight the cell. It doesn't look like there is a good way to forward the touches, so I'd imagine you will have to handle the highlighting manually if you want that to appear. You could do it through a separate method than setHighlighted, or try to use the cell.highlighted property to do your drawing.

Here is an example I came up with that worked for my UICollectionView

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
//    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
//        return;
//    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
            if (!cell.highlighted) {
                [cell setHighlighted:YES];
            }
            return;
        }
        [cell setHighlighted:NO];
        // do stuff with the cell
    }
}
Alex
  • 3,861
  • 5
  • 28
  • 44
  • Thank you, I've already done something like that, but this is working only for longPressing and not for tapping. If I put also a UITapGestureRecognizer on my collectionView, then didSelectRowAtIndexPath doesn't work anymore... – andreacipriani Nov 10 '15 at 17:10
  • You can call [self.collectionView didSelectRowAtIndexPath:indexPath] from the UITapGestureRecognizer selector, as well as doing the highlighting you want – Alex Nov 10 '15 at 17:19