iOS 自定义布局 - 继承UICollectionViewFl
2017-09-11 本文已影响0人
小的小碰撞
- 目的
![](https://img.haomeiwen.com/i2699808/50c6deba83530ecc.jpg)
![](https://img.haomeiwen.com/i2699808/2d9b72c2c95af06a.png)
-
UICollectionViewLayoutAttributes
B907E982F57A3D741C297403ACCDF6F8.jpg
重写layoutAttributesForElementsInRect:方法
- 作用:
- 这个方法的返回值是个数组
- 这个数组中存放的都是UICollectionViewLayoutAttributes对象
- UICollectionViewLayoutAttributes对象决定了cell的排布方式(frame等)
![](https://img.haomeiwen.com/i2699808/2692e2946e465b46.jpg)
- 绝对值
![](https://img.haomeiwen.com/i2699808/1659622e19e6efa6.jpg)
重写shouldInvalidateLayoutForBoundsChange:方法
- 作用:如果返回YES,那么collectionView显示的范围发生改变时,就会重新刷新布局
- 一旦重新刷新布局,就会按顺序调用下面的方法:
- prepareLayout
- layoutAttributesForElementsInRect:
![](https://img.haomeiwen.com/i2699808/becbcf8ee7ce00dd.jpg)
- 示意图
![](https://img.haomeiwen.com/i2699808/f872afd55de07ef6.jpg)
- 理解
![](https://img.haomeiwen.com/i2699808/5429be458383ee49.jpg)
重写targetContentOffsetForProposedContentOffset:withScrollingVelocity:方法
- 作用:返回值决定了collectionView停止滚动时最终的偏移量(contentOffset)
- 参数:
- proposedContentOffset:原本情况下,collectionView停止滚动时最终的偏移量
-
velocity:滚动速率,通过这个参数可以了解滚动的方向
D06BFC37B363737EF6C5E76D2E9D621C.jpg
![](https://img.haomeiwen.com/i2699808/0ba2dab10d93a732.jpg)
![](https://img.haomeiwen.com/i2699808/1af530df4d94b1ec.jpg)
重写prepareLayout方法
- 作用:在这个方法中做一些初始化操作
- 注意:一定要调用[super prepareLayout]
![](https://img.haomeiwen.com/i2699808/b3e14b31be31c231.jpg)
![](https://img.haomeiwen.com/i2699808/c7932a6ae3202bbb.jpg)
-
枚举类型定义成常量
D78082CEB322FD5CE369C9B533C356DE.jpg