实现一个自定义Layout的正常做法就是继承与UICollectionViewlayout,论文,然后重载以下方法
三个必须手动重载的方法
1.-(CGSize)collectionViewContentSize
返回可见区域内的大小
2.-(NSArray *)layoutAttributesForElementsInRect: (CGRect)rect
返回的是包含UICollectionViewlayoutAttributes对象的数组,该对象可以是cell,开题报告,supplementary或装饰视图
返回对应indexPath下cell的布局属性
-(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath: (NSIndexPath _)indexPath
返回对应indexpath下对应的追加视图属性 (没有可不重载)
-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind: (NSString _)kind atIndexPath: (NSIndexPath *)indexPath
返回对应indexpath下对应的追加视图属性 (没有可不重载)
-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind: (NSString_)decorationViewKind atIndexPath: (NSIndexPath _)indexPath
3.-(BOOL)shouldInvalidateLayoutForBoundsChange: (CGRect)newBounds
当边界发生变化时,也就是滚动的时候,如果返回Yes,就是会一直调用,而且不断刷新重新计算布局信息
需要详细分析的请戳这:传送门
github地址请戳这: