UICollectionView实现瀑布流

在iOS中可以实现瀑布流的目前已知的有2种方案:

  1. 使用UIScrollView自己封装一套,这种方案是应用于iOS6之前的,因为iOS6才出来UICollectionView,不过现在这种方案已经不怎么用了,还得自己封装。而且自己封装的性能不一定有系统的要好。
  2. 使用系统自带的UICollectionView,然后自定义layout,自己实现瀑布流效果

本文中我们介绍第二种实现方案
首先我们需要自定义一个继承于UICollectionViewLayout的layout,然后需要重写四个方法:

  1. (void)prepareLayout
  2. (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  3. (UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath
  4. (CGSize)collectionViewContentSize

第一个方法是做一些初始化的操作,这个方法必须先调用一下父类的实现
第二个方法返回的是一个装着UICollectionViewLayoutAttributes的数组
第三个方法返回indexPath位置的UICollectionViewLayoutAttributes
第四个方法是返回UICollectionView的可滚动范围

如何实现瀑布流

首先我们需要明白一点瀑布流的排列,瀑布流是大小不规则的一些控件分布在手机屏幕上面,然后肯定有长度高的也有矮的(就像人的身高一样,哈哈哈),当排满第一排的时候就会往下继续排,那么这个应该往哪里放呢,==答案就是把它放到第一排最短的那个下面==,以此类推,按照这个规律排列下去。
明白了这一点我们接下来就该写代码了
首先我们创建两个数组一个装着cell的布局属性,另一个装着当前cell的总高度

//c存放所有cell的布局属性
@property (nonatomic, strong) NSMutableArray *attrsArray;
//存放所有列的当前高度
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 内容的高度 */
@property (nonatomic, assign) CGFloat contentHeight;

  

- (void)prepareLayout
{
[super prepareLayout]; self.contentHeight = 0; //清除之前计算的所有高度,因为刷新的时候回调用这个方法
[self.columnHeights removeAllObjects];
for (NSInteger i = 0; i < DefaultColumnCpunt; i++) {
[self.columnHeights addObject:@(self.edgeInsets.top)];
} //把初始化的操作都放到这里
[self.attrsArray removeAllObjects]; //开始创建每一个cell对应的布局属性
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (NSInteger i = 0; i < count; i++) {
// 创建位置
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
// 获取indexPath位置cell对应的布局属性
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.attrsArray addObject:attrs];
}
}

  首先把cell的高度设置为self.edgeInsets.top不然这里会崩溃。然后取出来item的个数,然后循环取出每个item的UICollectionViewLayoutAttributes,然后把它加入到attsArray,然后在- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect这个方法中直接返回attrsArray即可。

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; CGFloat collectionViewW = self.collectionView.frame.size.width; CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right -(self.columnCount - 1) * self.columnMargin) / self.columnCount; CGFloat h = [self.delegate WaterFlowLayout:self heightForRowAtIndexPath:indexPath.item itemWidth:w]; NSInteger destColumn = 0; CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 0; i < self.columnCount; i++) {
CGFloat columnHeight = [self.columnHeights[i] doubleValue]; if (minColumnHeight > columnHeight) {
minColumnHeight = columnHeight;
destColumn = i;
}
} CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
CGFloat y = minColumnHeight;
if (y != self.edgeInsets.top) {
y += self.rowMargin;
} attrs.frame = CGRectMake(x, y, w, h); self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame)); CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
if (self.contentHeight < columnHeight) {
self.contentHeight = columnHeight;
}
return attrs; }

  上面这个方法是计算item的位置的代码,首先取出来indexPathUICollectionViewLayoutAttributes对象,然后取出来w,h,核心代码如下:

NSInteger destColumn = 0;

    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 0; i < self.columnCount; i++) {
CGFloat columnHeight = [self.columnHeights[i] doubleValue]; if (minColumnHeight > columnHeight) {
minColumnHeight = columnHeight;
destColumn = i;
}
} CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
CGFloat y = minColumnHeight;
if (y != self.edgeInsets.top) {
y += self.rowMargin;
}

  首先弄了一个标示destColumn来做记录是那一列的,然后定义一个minColumnHeight为最小的列的高度,取出来self.columnHeights[0]的高度,这里默认为它就是最小的,然后进行for循环遍历,取出来i位置上面的高度,如果这个值小于之前的minColumnHeight,那么取出来的这个高度就是最小的高度了,然后把i的值赋值给destColumn,然后x的值就是上面代码中的相加的结果,y的值就是继续加上间距

- (CGSize)collectionViewContentSize
{
// CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
//
// for (NSInteger i = 1; i < DefaultColumnCpunt; i++) {
// // 取得第i列的高度
// CGFloat columnHeight = [self.columnHeights[i] doubleValue];
//
// if (maxColumnHeight < columnHeight) {
// maxColumnHeight = columnHeight;
// }
// }
return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);
}

  传送门:gitHub

OC-UICollectionView实现瀑布流的更多相关文章

  1. iOS开发-UICollectionView实现瀑布流

    关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的 ...

  2. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  3. 利用UICollectionView实现瀑布流

    利用UICollectionView实现瀑布流通过自定义布局来实现. - 自定义类继承UICollectionViewLayout: 必须重写的方法有: //决定每个item的位置: - (nulla ...

  4. iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)

    一个瀑布流的实现有三种方式: 继承自UIScrollView,仿写UITableView的dataSource和delegate,创造一个缓存池用来实现循环利用cell 写多个UITableview( ...

  5. iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流

    在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...

  6. 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》

    一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...

  7. IOS 瀑布流UICollectionView实现

    IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...

  8. OC - 29.自定义布局实现瀑布流

    概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...

  9. UICollectionView 很简单的写个瀑布流

    你项目中要用到它吗? 可能会在你的项目中用到这玩意,最近也是要用就简单的写了一个 Demo.没多少代码,就不放Git了,下面会详细点的说说代码的,要还有什么问题的小伙伴可以直接Q我,也可以把Demo发 ...

随机推荐

  1. crontab问题处理

    用pyhton写了一些爬虫,由于数据量比较大,需要跑的时间也比较长,所以将代码部署到服务器上.选择用crontab完成爬虫的定时爬取数据,这样避免了人工的干预,减少一些人为错误.但在部署crontab ...

  2. 在PHP中连接数据库的八大步骤

                                                                                   连接数据库的八大步骤  Step1:链接数 ...

  3. TCP连接中time_wait在开发中的影响-搜人以鱼不如授之以渔

    根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL(Max Segment Lifetime),T ...

  4. [原]浅谈vue过渡动画,简单易懂

    在vue中什么是动画 开始先啰嗦一下,动画的解释(自我理解

  5. java执行程序的内存分析系列专栏

    近段时间研究了java的程序执行过程中的内存分配,收获颇多,解决了我最近时间学习java的很多困惑点.写java内存分析系列的目的主要有两个,一来是希望给像我一样的java初学者一定的启发,希望也能解 ...

  6. 探索Windows命令行系列(6):活用批处理解决实际问题

    1.批量修改文件名 2.批量重启服务 3.全盘搜索指定文件 3.1.全盘搜索名称为 mm.jpg 的文件,获取其全路径 3.2.查找系统中所有名称以 .docx 结尾的文件 4.调用可执行程序 4.1 ...

  7. (cljs/run-at (JSVM. :all) "一起实现柯里化")

    前言  习惯了Ramda.js就会潜意识地认为函数均已柯里化,然后就可以随心所欲的用函数生成函数,或者使用compose组合多个函数来生成一个新函数.如下 const f = a => b =& ...

  8. OJ2237第k小数题解

    题目描述: 有n个数,请你找出第k小的数. 输入描述: 第一行有2个正整数n,k(n,k<=10^7)第二行有n个非负数ai(ai<=10^5) 输出描述: 输出第k小的数. 输入样例: ...

  9. BufferedWriter

    package JBJADV003;import java.io.*;public class BufferedWriterTest { public static void main(String[ ...

  10. vijos1046题解

    题目: 学校里面有N个景点.两个景点之间可能直接有道路相连,用Dist[I,J]表示它的长度:否则它们之间没有直接的道路相连.这里所说的道路是没有规定方向的,也就是说,如果从I到J有直接的道路,那么从 ...