关于自定义UICollectionViewLayout

自定义UICollectionViewLayout可以通过重写一下几种方法实现:

// 准备布局,布局配置数据,布局前会调用这个方法,注意要调用 [super prepareLayout];
- (void)prepareLayout;
// 返回collectionView的可以滚动的区域大小
- (CGSize)collectionViewContentSize;
// 返回所有的布局信息
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
// 返回indexPath对应的cell的布局信息
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

还有两个没有用到过,留着

// 返回indexPath处的header或footer的布局信息
-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
// 返回indexPath处的装饰的布局信息
-(UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

目前写了三种。效果如下:
Demo地址

瀑布流.gif

多行横向整页滚动.gif

一行居中放大.gif

参考此文章实现
参考此文章实现

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容