//自定义布局,继承于UICollectionViewLayout

#import <UIKit/UIKit.h>

@class WaterFlowLayout;

@protocol  WaterFlowLayoutDeleagre ;

@interface WaterFlowLayout : UICollectionViewLayout

@property (nonatomic,weak)id<WaterFlowLayoutDeleagre>delegate;

@end

@protocol WaterFlowLayoutDeleagre <NSObject>

@required

- (CGFloat)collectionView:(__kindof UICollectionView *)collectionView heightForItemAtIndexPath:(NSIndexPath *)indexPath;

@end

-------------------------------------------------------------------------

#import "WaterFlowLayout.h"

static  CGFloat const  coloumMarginDefault = 10;  //间隙

static NSInteger const coloumDefault = 3; //这里可以更改列数

static CGFloat const rowMaginDegault = 10;   //行之间的间隙

@interface WaterFlowLayout ()

@property (nonatomic,strong)NSMutableArray *   attris;

@property (nonatomic,strong)NSMutableArray * cloumsHeight;

@end

@implementation WaterFlowLayout

//记录没列cell的最大Y值

- (NSMutableArray *)cloumsHeight

{

if (_cloumsHeight == nil) {

_cloumsHeight = [NSMutableArray array];

}

return _cloumsHeight;

}

//存放每个cell的属性值

- (NSMutableArray *)attris

{

if (_attris == nil) {

_attris = [NSMutableArray array];

}

return _attris;

}

//准备开始布局

- (void)prepareLayout

{

[super prepareLayout];

//布局开始之前清空所有的列高度

[self.cloumsHeight removeAllObjects];

//为列高度添加初始值

for (int i=0; i<coloumDefault; i++) {

[self.cloumsHeight addObject:@0];

}

//获取对应组的cell个数

NSInteger count  = [self.collectionView  numberOfItemsInSection:0];

NSMutableArray * atts = [NSMutableArray array];

//创建cell的布局属性

for (int i=0; i<count; i++) {

//获取collectionview中对应indexpath的cell属性

UICollectionViewLayoutAttributes * attributes   =   [self  layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

[atts addObject:attributes];

}

self.attris = atts;

}

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

//返回所有cell的布局属性

return   self.attris;

}

- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

{

//初始化布局属性

UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

//让纪律每列高度中的数组的第一个数据为最小值

CGFloat minHeight = [self.cloumsHeight[0]  floatValue];

//记录的列号

NSInteger   cloum  =0;

//取出最小高度的列

for ( NSInteger i=1; i<self.cloumsHeight.count; i++) {

if (minHeight > [self.cloumsHeight[i]  floatValue]) {

minHeight = [self.cloumsHeight[i]  floatValue];

cloum =i;

}

}

//计算宽度

CGFloat W = (self.collectionView.frame.size.width - coloumMarginDefault * (coloumDefault + 1))/ coloumDefault;

//计算高度

CGFloat H = [self.delegate collectionView:self.collectionView heightForItemAtIndexPath:indexPath];

//计算cell的x值

CGFloat X = coloumMarginDefault + (W +  coloumMarginDefault) *cloum;

//计算cell的Y值

CGFloat Y = minHeight + rowMaginDegault;

attributes.frame = CGRectMake(X, Y, W, H);

//更新记录每列高度的数组

[self.cloumsHeight   replaceObjectAtIndex:cloum withObject:@(CGRectGetMaxY(attributes.frame) )];

return attributes;

}

//返回collectionview的滚动范围

- (CGSize)collectionViewContentSize

{

CGFloat maxHeight = 0;

for (int i=0; i<self.cloumsHeight.count; i++) {

if (maxHeight < [self.cloumsHeight[i]  floatValue]) {

maxHeight =[self.cloumsHeight[i]  floatValue];

}

}

return     CGSizeMake(0, maxHeight);

}

@end

-----------------------------------------------------------------------------------------------如此即可------------------------------------------------

iOS开发瀑布流的实现的更多相关文章

  1. ios开发瀑布流框架的应用

    一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议 二:代码: #import "HMShopsViewController.h" #import "HMShopC ...

  2. ios开发瀑布流框架的封装

    一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...

  3. iOS基础 - 瀑布流

    一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...

  4. iOS横向瀑布流的封装

    前段时间, 做一个羡慕, 需要使用到瀑布流! 说道瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹 ...

  5. ios图片瀑布流代码

    ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html

  6. iOS - UICollectionView 瀑布流 添加表头视图的坑

    UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...

  7. 堆糖瀑布流完整解决方案(jQuery)

    2010年堆糖创办以来,网站界面经历过3-5次重大改版,logo也曾更换过两次,早期蓝红相间三个圈的logo恐怕很少有人记得了.与此同时,前端 js 框架也在默默的更新换代.最早堆糖上线时,js 采用 ...

  8. jQuery动态网格瀑布流插件Masonry

    Masonry是一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发瀑布流界面效果.和CSS中float的效果不太一样的地方在于,float先水平排列,然后再垂直排列,使用Masonr ...

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

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

随机推荐

  1. RHEL账号总结一:账号的分类

    账号是一种用来记录单个用户或者多个用户的数据.RHEL中每一个合法的用户都必须拥有账号,才能使用RHEL. 在RHEL上的账号可以分为两类: 用户账号:用来存储单一用户的数据,你也可以使用一个用户账号 ...

  2. 基于python第三方requests 模块的HTTP请求类

    使用requests模块构造的下载器,首先安装第三方库requests pip install requests 1 class StrongDownload(object): def __init_ ...

  3. CentOS 7安装redis及php扩展

    安装remi源 # wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm # rpm -Uvh remi-release-7 ...

  4. linux操作系统简单使用

    文章将包含几个内容: linux简介 linux操作系统的安装简述 linux操作系统的磁盘文件结构 linux操作系统中的文件操作 linux中的用户管理 网络配置管理 常用系统管理指令 linux ...

  5. 洛谷-神奇的幻方-NOIP2015提高组复赛

    题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,--,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. ...

  6. Openjudge-NOI题库-蛇形填充数组

    题目描述 Description 用数字1,2,3,4,...,n*n这n2个数蛇形填充规模为n*n的方阵. 蛇形填充方法为: 对于每一条左下-右上的斜线,从左上到右下依次编号1,2,...,2n-1 ...

  7. redis的持久化 rdb和aof

    1.rdb(Redis DataBase) 当满足条件时,redis单独会fork(创建)一个新的线程,会先将内存中的数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次已经持久化 ...

  8. Php 关于构造函数

    子类在继承父类后,会拥有父类的属性和方法,这是继承的特性. 子类在构造函数会首先调用父类的构造函数来实例化父类的属性,然后调用子类的构造函数,一般你不写,并不表示没有调用,而是首先调用了父类的无参构造 ...

  9. StringMVC(拦截器)

    单个拦截器 使用jar包 创建FirstController.java @Controller public class FirstController { @RequestMapping(" ...

  10. Mysql的转义字符

    Mysql的转义字符是"\",即反斜杠,在INSERT语句中,如果被插入的文本中包含反斜杠,那么反斜杠会被吃掉.例如: INSERT INTO tb (id,json) VALUE ...