Skip to content

NohEunTae/VisibilityTrackableCollectionView

Repository files navigation

VisibilityTrackableCollectionView

Swift swift-package-manager license

The easiest way to find fully visible CollectionView's Cell & ReusableView while scrolling

At a Glance

Detect CollectionView's Cell or ReusableView fully visible

ezgif com-video-to-gif-2

And also can detect nested CollectionView's fully visibility

ezgif com-video-to-gif-3

Getting Started

Step 1. Use VisibilityTrackableCollectionView

use VisibilityTrackableCollectionView and set delegate just like UICollectionView

class ViewController: UIViewController {
    @IBOutlet weak var collectionView: VisibilityTrackableCollectionView!
    collectionView.delegate = self
}

Step 2. Setting Properties

you can set boundary that to be detected

default boundary is superior viewController's view (not safeArea)

collectionView.setBoundary(.init(view: someView, mode: .safeArea))

Step 3. Adopt CollectionViewDelegateVisibleItems with UICollectionViewDelegate just like UICollectionViewDelegateFlowLayout

extension ViewController: CollectionViewDelegateVisibleItems {

    func collectionView(_ collectionView: UICollectionView, firstTimeOfFullyVisibleItems items: [IndexPath]) {
    }
    
    func collectionView(_ collectionView: UICollectionView, allOfFullyVisibleItems items: [IndexPath]) {
    }
    
    func collectionView(_ collectionView: UICollectionView, supplementaryElementOfKind kind: String, firstTimeOfFullyVisibleItems items: [IndexPath]) {
    }
    
    func collectionView(_ collectionView: UICollectionView, supplementaryElementOfKind kind: String, allOfFullyVisibleItems items: [IndexPath]) {
    }    

}

Tips and Tricks

  • I want to refresh firstTimeOfFullyVisibleItems
collectionView.refreshSeenData()
  • I want to use nesting

just use VisibilityTrackableCollectionView for nesting.

It's designed for reuse, so you don't have to worry about firstTimeOfFullyVisibleItems.

  • I want to refresh nested VisibilityTrackableCollectionView when superior has been refreshed

just call superior VisibilityTrackableCollectionView's refreshSeenData() that causes all of internal data refreshed.

Installation

Use Swift Package Manager (SPM)

License

VisibilityTrackableCollectionView is under MIT license.