测试已测试 | ✓ |
Lang语言 | SwiftSwift |
许可 | MIT |
Released最新版本 | 2017年9月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✗ |
由Raffaele Cerullo维护。
STWCollectionView 是一个易于创建和管理的 UICollectionView 子类,用于带有轮播效果集合,并且非常简单自定义。
您可以设置同时显示多少个单元格,或者给单元格一个固定的大小,集合会自动设置同时可视的单元格。
您还可以设置单元格之间的空间以及垂直和水平填充,这样您可以看到相邻单元格的一小部分。
此外,STWCollectionView 有一个代理来更好地在滚动阶段管理单元格,例如:当前可见的 indexPaths 和根据中心定位的百分比。
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
单元格从顶部和底部边界的距离。
var verticalPadding: CGFloat { get set }
// default: 20
horizontalPadding
单元格从左边和右边界的距离。
var horizontalPadding: CGFloat { get set }
// default: 20
itemSpacing
单元格之间的距离。
var itemSpacing: CGFloat { get set }
// default: 20
fixedCellsNumber
同时可视的单元格数量。
var fixedCellsNumber: Int { get set }
// default: 1
fixedCellSize
单元格的固定大小。
var fixedCellSize: CGSize? { get set }
forceCentered
强制 STWCollectionView 的 contentInset,以便首尾项目居中。
var forceCentered: Bool { get set }
// default: false
direction
滚动方向。
(支持垂直)
var direction: UICollectionViewScrollDirection { get set }
// default: .horizontal
STWCollectionView 获取属性:
currentVisibleIndexPaths
当前可见条目的 indexPaths。
var currentVisibleIndexPaths: [IndexPath] { get }
currentPage
中心的当前索引。
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 文件。