Search code examples
swiftuicollectionviewuicollectionviewcelluicollectionviewlayout

Check if a collectionViewCell is showing more than 50% on collectionView


i got a UICollectionView with (3*3 Grid), so when i scroll up and down, is there a way i can detect if the collection view cell is displaying more than 50% of its height on the screen?

let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.minX, y: visibleRect.midY)
let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)

I have tried this but it doesn't seems to work. Does anyone has solution for this?


Solution

  • You can ask for

    @property(nonatomic) CGPoint contentOffset; 
    

    of UIScrollView because UICollectionView's inherits from them.

    and don't forget your Cell's have a CALayer that knows the visible Rectangle as well.

    CGRect visiblerect = cell.layer.visibleRect;
    if (visiblerect.size.height > (cell.frame.size.height * 0.5)) {
        // do stuff when cell is more visible then half its size
    }