- iOS 9.0+
- Xcode 9.4+
- Swift 4.1+
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
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
infiniteScrollingBehavior = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: false)
collectionView.delegate = infiniteScrollingBehavior
collectionView.dataSource = infiniteScrollingBehavior
Implement the protocols InfiniteScrollingBehaviorDataSource and InfiniteScrollingBehaviorDelegate
// 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
}
}
To force infinite Scrolling pass true in the constructor of the InfiniteScrollingBehavior.
infiniteScrollingBehavior = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: true)