iOS开发瀑布流的实现
//自定义布局,继承于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开发瀑布流的实现的更多相关文章
- ios开发瀑布流框架的应用
一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议 二:代码: #import "HMShopsViewController.h" #import "HMShopC ...
- ios开发瀑布流框架的封装
一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...
- iOS基础 - 瀑布流
一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...
- iOS横向瀑布流的封装
前段时间, 做一个羡慕, 需要使用到瀑布流! 说道瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹 ...
- ios图片瀑布流代码
ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html
- iOS - UICollectionView 瀑布流 添加表头视图的坑
UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...
- 堆糖瀑布流完整解决方案(jQuery)
2010年堆糖创办以来,网站界面经历过3-5次重大改版,logo也曾更换过两次,早期蓝红相间三个圈的logo恐怕很少有人记得了.与此同时,前端 js 框架也在默默的更新换代.最早堆糖上线时,js 采用 ...
- jQuery动态网格瀑布流插件Masonry
Masonry是一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发瀑布流界面效果.和CSS中float的效果不太一样的地方在于,float先水平排列,然后再垂直排列,使用Masonr ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
随机推荐
- java基础练习 2
public class Second { /* * 打印出杨辉三角形(要求打印出10行如下图) */ public static void main(String[] args){ int i,j, ...
- SQL注入(一) - 入门篇
什么是SQL注入 可能大家还不是对SQL注入这个概念不是很清楚,简单地说,SQL注入就是攻击者通过正常的WEB页面,把自己SQL代码传入到应用程序中,从而通过执行非程序员预期的SQL代码,达到窃取数据 ...
- 第11章:DOM扩展
- D3.js:Update、Enter、Exit
Update.Enter.Exit 是 D3 中三个非常重要的概念,它处理的是当选择集和数据的数量关系不确定的情况. 如果数组为 [3, 6, 9, 12, 15],将此数组绑定到三个 p 元素的选择 ...
- iOS Quartz2D画图
对于刚接触Quartz2D的同学来说,先了解 上下文 的概念,再从最基础的画线来具体体验Quartz2D的画图步骤 介绍Quart2D :是苹果官方的二维(平面)绘图引擎,同时支持iOS和macOS系 ...
- >> 计算机的数据表示
1. 采用二进制 2. 负数采用补码表示 3. 乘法处理 4. 浮点数
- Long类型比较大小,long型和Long型区别
今天写代码发现发现本地程序是正常的,但是发送到测试环境就不正常了,本着对数据的怀疑态度链接了测试数据库,调试程序发现,确实是数据问题,然后数据出现在什么地方呢?才发现是在判断用户所属的teamGrou ...
- Logstash安装搭建(一)
Logstash是一个具有实时管道的开源数据收集引擎.可以动态地统一不同来源的数据,并将数据归到不同目的地.也是一个管理事件和日志工具.你可以用它来收集日志,分析它们,并将它们储存起来以供以后使用. ...
- C# 语言规范_版本5.0 (第19章 附录A_文档注释)
A. 文档注释 C# 提供一种机制,使程序员可以使用含有 XML 文本的特殊注释语法为他们的代码编写文档.在源代码文件中,可以使用特定形式的注释来指导工具从这些注释及其后的源代码元素生成 XML.使用 ...
- HOOK(钩子)函数
安装.卸载钩子的相关函数 钩子类型 按功能分: 1.WH_CALLWNDPROC和WH_CALLWNDPROCRET:使你可以监视发送到窗口过程的消息3.WH_DEBUG 调试钩子4.WH_FO ...