ExpandableColumnLayout 1.0.1

ExpandableColumnLayout 1.0.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布2016年2月
SPM支持SPM

John Volk维护。



  • 作者:
  • John Volk

ExpandableColumnLayout

一个自定义的UICollectionViewLayout,它提供具有可选可展开抽屉功能的可伸缩列布局。

  • 每个部分都可以有任意的列数
  • 指定单元高度或每个项的精确高度
  • 通过吸引人的抽屉式动画展开或收缩部分

demo

安装

ExpandableColumnLayout可以通过CocoaPods获得。要安装它,只需将以下行添加到Podfile中

pod "ExpandableColumnLayout"

使用方法

ExpanableColumnLayout依赖于ExpandableColumnLayoutDelegate的一个实例来提供构建布局所需的信息。ExpandableColumnLayoutDelegateUICollectionViewDelegate的子类型,因此继承自该协议的所有必需行为。此外,它还添加了一些自己的必需行为。

必须实现以下两个方法

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。这是示例项目中使用的方法。当您继承自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类为您完成大部分工作。

此外,您需要在其实现中提供适当的ExpandableColumnLayoutHeaderKindExpandableColumnLayoutSectionBackgroundKind辅助视图实例

public func collectionView(collectionView: UICollectionView,
  viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

帘布布局需要为每UICollectionView中的每个部分提供一个ExpandableColumnLayoutSectionBackgroundKind实例以支持抽屉动画。

作者

John Volk, [email protected]

许可证

ExpandableColumnLayout 在MIT许可证下可用。更多信息请参阅LICENSE文件。