简体   繁体   中英

UICollectionView inside UICollectionViewCell - empty area cancels touches

I have a UICollectionView (A) with photo gallery thumbnails inside a UICollectionViewCell of another UICollectionView (B).

I'd like to achieve this behavior:

  • when a user taps on the thumbnail (A's cell), detail of the photo is opened (no problem here)
  • when a user taps on empty space of the UICollectionView (A) I'd like to open (B) cell detail. But the empty area of the UICollectionView (A) cancels user touches and doesn't forward them to the (B) cell.

I have:

_collectionView.canCancelContentTouches = NO;
_collectionView.delaysContentTouches = NO;

and using autolayout.

Thank you for your help!

I found a solution. I subclassed UICollectionView and then override hitTest method.

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView* view = [super hitTest:point withEvent:event];

    if (view == self) {

        return self.superview;

    }

    else {

         return view;

    }
}

swift version

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    let v = super.hitTest(point, withEvent: event)
    return v == self ? superview : v
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM