Skip to content

Latest commit

 

History

History
34 lines (22 loc) · 2.06 KB

iOS之自定义UICollectionViewLayout.md

File metadata and controls

34 lines (22 loc) · 2.06 KB

iOS之自定义UICollectionViewLayout

Core Layout Process

UICollectionView进行布局时需要借助UICollectionViewLayout,两者交互的过程大致分三个阶段:

  1. 首先执行UICollectionViewLayout的prepare方法,做数据准备
  2. UICollectionView通过UICollectionViewLayout 的collectionViewContentSize属性或方法获取滚动视图的内容尺寸
  3. UICollectionView通过UICollectionViewLayout的layoutAttributesForElementsInRect: 方法获取正在展示中的cell的样式等信息

如何自定义布局

通过重写上面三阶段不同的方法,我们可以自定义布局

  • prepare阶段,我们可以做一些数据缓存工作,方便后序使用
  • 比较关键的是后序layoutAttributesForElementsInRect阶段
    • 该阶段我们可能根据需要为cell、supplementaryView、decorationView创建不同的layoutAttribute对象
    • 通常的做法可能是,通过datasource遍历寻找那些需要调整样式的cell(比如可视区域内的cell),为每一个cell创建一个layoutAttribute,放入数组作为结果返回
  • 因为layoutAttributesForElementsInRect可能会频繁执行,所以要考虑性能问题
  • layoutAttributesForElementsInRect:是针对多个元素创建laoutAttrbute,还要考虑可能也要为单个的item提供layoutAttribute的情况,就是类似layoutAttributesForItemAtIndexPath:这一类的方法

自定义布局可以做哪些事

  • 可以看一下UICollectionViewLayoutAttributes中支持哪些属性,比如zIndex、transform
  • 可以自定义插入、删除动画
  • 瀑布流效果,在社交软件中很常用
  • 通过创建UICollectionViewLayoutAttributes的子类+增加自定义背景色属性,为collectionView添加sectionBacgroundColor

参考