STWCollectionView 0.1.2

STWCollectionView 0.1.2

测试已测试
Lang语言 SwiftSwift
许可 MIT
Released最新版本2017年9月
SwiftSwift 版本4.0
SPM支持 SPM

Raffaele Cerullo维护。





STWCollectionView 是一个易于创建和管理的 UICollectionView 子类,用于带有轮播效果集合,并且非常简单自定义。
您可以设置同时显示多少个单元格,或者给单元格一个固定的大小,集合会自动设置同时可视的单元格。
您还可以设置单元格之间的空间以及垂直和水平填充,这样您可以看到相邻单元格的一小部分。
此外,STWCollectionView 有一个代理来更好地在滚动阶段管理单元格,例如:当前可见的 indexPaths 和根据中心定位的百分比。

![Demo](screenshots/example.gif)

安装

CocoaPods

pod 'STWCollectionView'

使用

设置方式类似于 UICollectionView

let stwCollectionView = STWCollectionView()

override func viewDidLoad() {
  super.viewDidLoad()

  // delegate & data source
  stwCollectionView.delegate = self
  stwCollectionView.dataSource = self

  // layout subviews
  ...

  // register collection cells
  stwCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)

}

// datasource extensions
...

代理

除了 UICollectionViewDelegate 方法外,STWCollectionView 通过 STWCollectionViewDelegate(它是 UICollectionViewDelegate 协议的子类)有三个其他方法来管理滚动

extension ViewController: STWCollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
		// Scroll to selected cell
        stwCollectionView.scrollTo(indexPath: indexPath, animated: true)
    }
    
    func collectionViewDidEndDeceleratingWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
        print("indexPaths after Decelerating: \(indexPaths)")
        print("percentages after Decelerating: \(percentages)")
    }
    
    func collectionViewDidEndScrollingAnimationWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
        print("indexPaths after Scrolling Animation: \(indexPaths)")
        print("percentages after Scrolling Animation: \(percentages)")
    }
    
    func collectionViewDidScrollWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
        print("indexPaths during Scrolling: \(indexPaths)")
        print("percentages during Scrolling: \(percentages)")        
    }
}

自定义

您可以为所有需求自定义 STWCollectionView

STWCollectionView 设置属性:

  • verticalPadding 单元格从顶部和底部边界的距离。
    如果 STWCollectionView 是水平且具有固定单元格大小,则禁用

    var verticalPadding: CGFloat { get set }
    // default: 20
  • horizontalPadding 单元格从左边和右边界的距离。
    如果 STWCollectionView 是垂直且具有固定单元格大小,则禁用

    var horizontalPadding: CGFloat { get set }
    // default: 20
  • itemSpacing 单元格之间的距离。

    var itemSpacing: CGFloat { get set }
    // default: 20
  • fixedCellsNumber 同时可视的单元格数量。
    设置时将 fixedCellSize 设置为 nil

    var fixedCellsNumber: Int { get set }
    // default: 1
    
  • fixedCellSize 单元格的固定大小。
    设置时自动计算 fixedCellsNumber

    var fixedCellSize: CGSize? { get set }
    
  • forceCentered 强制 STWCollectionView 的 contentInset,以便首尾项目居中。
    仅在设置了 fixedCellSize 时使用

    var forceCentered: Bool { get set }
    // default: false
    
  • direction 滚动方向。
    (支持垂直)

    var direction: UICollectionViewScrollDirection { get set }
    // default: .horizontal
    

STWCollectionView 获取属性:

  • currentVisibleIndexPaths 当前可见条目的 indexPaths。

    var currentVisibleIndexPaths: [IndexPath] { get }
    
  • currentPage 中心的当前索引。
    如果 fixedCellsNumber 是偶数,则结果是两个值之间的 CGFloat

    var currentPage: CGFloat { get }
    

STWCollectionView 公共方法:

  • scrollTo(indexPath: animated:) 在特定的 indexPath 滚动 STWCollectionView。

    func scrollTo(indexPath:IndexPath, animated:Bool)
    

要求

此 Pod 需要 iOS 9.0 或更高版本的部署目标

作者

@Steewitter, [email protected]

许可

STWCollectionView 基于 MIT 许可证可用。有关更多信息,请参阅 LICENSE 文件。