一个自定义的UICollectionViewLayout
,它提供具有可选可展开抽屉功能的可伸缩列布局。
ExpandableColumnLayout
可以通过CocoaPods获得。要安装它,只需将以下行添加到Podfile中
pod "ExpandableColumnLayout"
ExpanableColumnLayout
依赖于ExpandableColumnLayoutDelegate
的一个实例来提供构建布局所需的信息。ExpandableColumnLayoutDelegate
是UICollectionViewDelegate
的子类型,因此继承自该协议的所有必需行为。此外,它还添加了一些自己的必需行为。
必须实现以下两个方法
func numberOfColumnsInCollectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
forSectionAtIndex section: Int) -> Int;
func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
sectionIsExpandedAtIndex section: Int) -> Bool;
此外,ExpandableColumnLayoutDelegate
实例必须为其每个部分的每个项提供精确高度或单元高度。有两种方法可以实现这一目标。根据您的应用程序选择最合适的。
optional func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
itemHasExactHeightAtIndexPath indexPath: NSIndexPath!) -> Bool;
optional func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
exactHeightForItemAtIndexPath indexPath: NSIndexPath!,
withWidth width: CGFloat) -> CGFloat;
或者
optional func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
unitHeightForItemAtIndexPath indexPath: NSIndexPath!) -> Int;
开始最快的办法是继承提供的ExpandableColumnViewController
。这是示例项目中使用的方法。当您继承自ExpandableColumnViewController
时,您需要重写两个方法。
public func numberOfItemsInExpandedSection(section: Int) -> Int {
...
}
public func viewForSupplementaryHeaderElementAtIndexPath(indexPath: NSIndexPath) -> UICollectionReusableView {
...
}
viewForSupplementaryHeaderElementAtIndexPath:
方法只需要在您不返回从非CGZero尺寸的可选实现返回的任何尺寸时重写
optional func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
sizeForHeaderInSection section: Int) -> CGSize;
如果您不想将自己绑定到特定的UIViewController
子类型,您可能需要多做一点工作。
首先,您将通过提供对对展开部分的信息进行账簿管理的实现来完成任何关于展开部分的信息
func collectionView(collectionView: UICollectionView,
layout expandableColumnLayout: ExpandableColumnLayout,
sectionIsExpandedAtIndex section: Int) -> Bool;
您可以使用提供的SectionExpander
类为您完成大部分工作。
此外,您需要在其实现中提供适当的ExpandableColumnLayoutHeaderKind
和ExpandableColumnLayoutSectionBackgroundKind
辅助视图实例
public func collectionView(collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
帘布布局需要为每UICollectionView
中的每个部分提供一个ExpandableColumnLayoutSectionBackgroundKind
实例以支持抽屉动画。
John Volk, [email protected]
ExpandableColumnLayout 在MIT许可证下可用。更多信息请参阅LICENSE文件。