自定義布局 - 繼承UICollectionViewFlowLayout
重寫prepareLayout方法
- 作用:在這個方法中做一些初始化操作
- 注意:一定要調(diào)用[super prepareLayout]
重寫layoutAttributesForElementsInRect:方法
- 作用:
- 這個方法的返回值是個數(shù)組
- 這個數(shù)組中存放的都是UICollectionViewLayoutAttributes對象
- UICollectionViewLayoutAttributes對象決定了cell的排布方式(frame等)
重寫shouldInvalidateLayoutForBoundsChange:方法
- 作用:如果返回YES,那么collectionView顯示的范圍發(fā)生改變時,就會重新刷新布局
- 一旦重新刷新布局,就會按順序調(diào)用下面的方法:
- prepareLayout
- layoutAttributesForElementsInRect:
重寫targetContentOffsetForProposedContentOffset:withScrollingVelocity:方法
- 作用:返回值決定了collectionView停止?jié)L動時最終的偏移量(contentOffset)
- 參數(shù):
- proposedContentOffset:原本情況下,collectionView停止?jié)L動時最終的偏移量
- velocity:滾動速率,通過這個參數(shù)可以了解滾動的方向
自定義布局 - 繼承UICollectionViewLayout
重寫prepareLayout方法
重寫layoutAttributesForElementsInRect:方法
重寫collectionViewContentSize方法
- 作用:返回collectionView的內(nèi)容大小
重寫layoutAttributesForItemAtIndexPath:方法
- 作用:返回indexPath位置對應(yīng)cell的布局屬性
demo下載:
https://github.com/Tuberose621/-CollectionViewLayout-CollectionViewFlowLayout-
說明:出自MJ筆記