Search code examples
iosuiviewuicollectionviewuicollectionviewlayouthittest

UICollectionView hitTest -> normal behavior


I overrode hitTest, and that works just fine. I want it to behave as if I hadn't overridden this method under certain conditions, and that's where the problem lies.

I'm using a subclassed UICollectionView to render cells over a MKMapView using a custom UICollectionViewLayout implementation. I needed to override hitTest in the UICollectionView subclass so that touch events can be passed to the mapView and it can be scrolled. That all works fine.

I have a toggle mechanism which animates between my UICollectionViewLayout (map) to a UICollectionViewFlowLayout (animate items on a map to a grid format). This works good too, but when I'm showing the flow layout, I want the user to be able to scroll the UICollectionView like a normal one (act as though hitTest isn't overridden). I can't figure out what to return in hitTest to have it's default behavior.

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    if(self.tapThrough == YES){
        NSArray *indexPaths = [self indexPathsForVisibleItems];
        for(NSIndexPath *indexPath in indexPaths){
            UICollectionViewCell *cell = [self cellForItemAtIndexPath:indexPath];
            if(CGRectContainsPoint(cell.frame, point)){
                return cell;
            }
        }
        return nil;
    } else {
        return ???
    }
}

I've tried returning a number of things. self, self.superview, etc... Nothing get it to behave normally (I cannot scroll the cells up and down).


Solution

  • If you want the normal behaviour of your hit test:

    return [super hitTest:point withEvent:event];
    

    This will return what the hit test usually returns when it is not overridden.