Markdownify
GAInfiniteCollectionKit-iOS

Give your collection view the infinite scrolling behavior.

Sponsor Version Author Swift Swift

Table of Contents

  1. Requirements
  2. Installation
  3. How to Use
  4. Advanced uses
  • iOS 9.0+
  • Xcode 9.4+
  • Swift 4.1+

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate GAInfiniteCollectionKit-iOS into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
  pod 'GAInfiniteCollectionKit-iOS'
end

Then, run the following command:

$ pod install

Step one:

Create instance of InfiniteScrollingBehavior and set the collection view delgate and dataSource to the InfiniteScrollingBehavior instance, The infinite Scrolling will work only if the numner of item is bigger then the frame of the collection view

Example:

infiniteScrollingBehavior   = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: false)
collectionView.delegate     = infiniteScrollingBehavior
collectionView.dataSource   = infiniteScrollingBehavior

Step two:

Implement the protocols InfiniteScrollingBehaviorDataSource and InfiniteScrollingBehaviorDelegate

Example:

// MARK: - InfiniteScrollingBehaviorDataSource
extension MyObject : GAInfiniteScrollingBehaviorDataSource
{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, withIndexForItem itemIndex: Int) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
        
        configor(cell: cell, in: itemIndex)
        return cell
    }
    
    func numberOfItems() -> Int
    {
        return array.count
    }
}

// MARK: - InfiniteScrollingBehaviorDelegate
extension MyObject : GAInfiniteScrollingBehaviorDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 150, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 10
    }
}

forceInfinite:

To force infinite Scrolling pass true in the constructor of the InfiniteScrollingBehavior.

Example:

infiniteScrollingBehavior   = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: true)