CollectionViewSlantedLayout 是 UICollectionViewLayout 的子类,允许在 UICollectionView 中显示倾斜的单元格。
特性
- 纯 Swift 4。
- 与每个 UICollectionView 兼容。
- 支持水平和垂直滚动。
- 动态单元格高度
- 完全可配置
安装
CocoaPods
CollectionViewSlantedLayout 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
use_frameworks!
pod 'CollectionViewSlantedLayout', '~> 3.1'
Carthage
您还可以通过 Carthage 进行安装。为此,请将以下内容添加到您的 Cartfile
github 'yacir/CollectionViewSlantedLayout'
使用方法
-
将
CollectionViewSlantedLayout
模块导入您的控制器import CollectionViewSlantedLayout
-
创建一个实例并将其添加到您的
UICollectionView
中。let slantedSayout = CollectionViewSlantedLayout() UICollectionView(frame: .zero, collectionViewLayout: slantedSayout)
-
使用
CollectionViewSlantedCell
类或继承它来自定义单元格。
在Examples文件夹中查找示例。
属性
-
slantingSize:
@IBInspectable var slantingSize: UInt
倾斜尺寸。此属性的默认值是
75
。 -
slantingDirection:
var slantingDirection: SlantingDirection
倾斜方向。此属性的默认值是
向上
。 -
slantingAngle:
fileprivate(set) var slantingAngle: CGFloat
倾斜角度(以弧度为单位)。此属性的值可以用作在
collectionView(_:cellForItemAt:)
方法实现中对单元格的内容视图应用旋转变换。if let layout = collectionView.collectionViewLayout as? CollectionViewSlantedLayout { cell.contentView.transform = CGAffineTransform(rotationAngle: layout.rotationAngle) }
-
scrollDirection:
var scrollDirection: UICollectionViewScrollDirection
网格的滚动方向。网格布局仅在单一轴上滚动,水平或垂直。此属性的默认值是
垂直
。 -
isFirstCellExcluded:
@IBInspectable var isFirstCellExcluded: Bool
将其设置为
true
以禁用第一个单元格的倾斜。此属性的默认值是false
。 -
isLastCellExcluded:
@IBInspectable var isLastCellExcluded: Bool
将其设置为
true
以禁用最后一个单元格的倾斜。此属性的默认值是false
。 -
lineSpacing:
@IBInspectable var lineSpacing: CGFloat
两个项目之间的间距。此属性的默认值是
10.0
。 -
itemSize:
@IBInspectable var itemSize: CGFloat
单元格的默认大小。如果代理未实现
collectionView(_:layout:sizeForItemAt:)
方法,倾斜布局将使用此属性中的值设置每个单元格的大小。这会导致所有单元格具有相同的大小。此属性的默认值是225
。 -
zIndexOrder:
var zIndexOrder: ZIndexOrder
布局中项的 zIndex 顺序。此属性的默认值是
升序
。
协议
CollectionViewDelegateSlantedLayout
协议定义了方法,使您能够与 CollectionViewSlantedLayout
对象协调以实现倾斜布局。 CollectionViewDelegateSlantedLayout
协议有以下几个方法
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: CollectionViewSlantedLayout,
sizeForItemAt indexPath: IndexPath) -> CGFloat
此方法请求委托为指定项的单元格提供大小。
如果您不实现此方法,倾斜布局将使用其 itemSize
属性中的值来设置项目的大小。您的实现方法可以返回一组固定的大小,或根据单元格的内容动态调整大小。
作者
致谢
本框架受到 这个原型 的启发,由 Matt Bridges 发布。
许可证
CollectionViewSlantedLayout 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。