GAInfiniteCollectionKit-iOS
给您的集合视图添加无限滚动行为。
目录
要求
- iOS 9.0+
- Xcode 9.4+
- Swift 4.1+
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理工具。您可以按照以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 GAInfiniteCollectionKit-iOS 集成到您的 Xcode 项目中,请在 Podfile 中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'GAInfiniteCollectionKit-iOS'
end
然后,运行以下命令
$ pod install
如何使用
第一步
创建 InfiniteScrollingBehavior 实例,并将集合视图代理和数据源设置到 InfiniteScrollingBehavior 实例中。只有在项目数量大于集合视图框架时,无限滚动才会生效。
示例
infiniteScrollingBehavior = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: false)
collectionView.delegate = infiniteScrollingBehavior
collectionView.dataSource = infiniteScrollingBehavior
第二步
实现 InfiniteScrollingBehaviorDataSource 和 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
}
}
高级用法
forceInfinite
要强制无限滚动,请在 InfiniteScrollingBehavior 的构造函数中传入 true。
示例
infiniteScrollingBehavior = InfiniteScrollingBehavior(collectionView: collectionView, dataSource: self, delegate: self, forceInfinite: true)