利用UICollectionView实现瀑布流通过自定义布局来实现。

- 自定义类继承UICollectionViewLayout;

必须重写的方法有:

//决定每个item的位置;

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

//布局layout

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

//滚动的范围,类似于scrollview的contentSize

- (CGSize)collectionViewContentSize;

@protocol WJLWaterFallLayoutDelegate;

@interface WJLWaterFallLayout : UICollectionViewLayout

@property(nonatomic,assign)int colCount;//列数
@property(nonatomic,assign)CGFloat colSpace;//列间距
@property(nonatomic,assign)CGFloat lineSpace;//行间距
@property(nonatomic,assign)UIEdgeInsets sectionInsets;
@property(nonatomic,weak) id <WJLWaterFallLayoutDelegate> delegate; @end @protocol WJLWaterFallLayoutDelegate <NSObject> @required
//通过代理得到每个item 的一个高度
- (CGFloat)collectionViewLayout:(WJLWaterFallLayout*)layout heightAtIndexPath:(NSIndexPath*)indexPath;
#import "WJLWaterFallLayout.h"

static const int colCount = ;//默认列数
static const CGFloat space = ;//默认间距 @interface WJLWaterFallLayout() @property(nonatomic,strong)NSMutableDictionary *maxDic;//储存列的长度 @end @implementation WJLWaterFallLayout - (instancetype)init{
if (self = [super init]) {
self.colCount = colCount;
self.lineSpace = space;
self.colSpace = space;
self.sectionInsets = UIEdgeInsetsMake(space, space, space, space);
self.maxDic = [NSMutableDictionary dictionary];
}
return self;
} - (void)prepareLayout{
[super prepareLayout];
}
/**
* 决定collectionview能滑动的范围
*/
- (CGSize)collectionViewContentSize{
__block NSString *maxCol = @"";
//找到最长的列
[self.maxDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] > [self.maxDic[maxCol] floatValue]) {
maxCol = key;
}
}];
CGFloat maxY = [self.maxDic[maxCol] floatValue]+self.lineSpace;
return CGSizeMake(, maxY) ;
}
/**
* 布局layout
*/
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
for (int i = ; i < self.colCount; i ++) {
NSString *key = [NSString stringWithFormat:@"%d",i];
self.maxDic[key] = @;
} NSMutableArray *arr = [NSMutableArray array];
int count = (int)[self.collectionView numberOfItemsInSection:]; for (int i = ; i < count ; i ++) {
UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:]];
[arr addObject:att];
} return arr;
}
/**
决定了每个item的位置,大小
*/
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
__block NSString *minCol = @"";
[self.maxDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] < [self.maxDic[minCol] floatValue] ) {
minCol = key;
}
}];
int col = [minCol intValue];
CGFloat nowY = [self.maxDic[minCol] floatValue];//当前列的y值 CGFloat itemW = (self.collectionView.frame.size.width - self.sectionInsets.left - self.sectionInsets.right - self.colSpace*(self.colCount - ))/(self.colCount);
CGFloat itemH = ;
if ([self.delegate respondsToSelector:@selector(collectionViewLayout:heightAtIndexPath:)]) {
itemH = [self.delegate collectionViewLayout:self heightAtIndexPath:indexPath];
}
CGFloat x = self.sectionInsets.left + (itemW + self.colSpace) * col;
CGFloat space = ; if (indexPath.row < self.colCount) {//第一行的时候y值
space = self.sectionInsets.top;
}else {
space = self.lineSpace;
}
CGFloat y = nowY + space; self.maxDic[minCol] = @(y + itemH);//更新此列的y值 UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(x, y, itemW, itemH); return attributes;
}
@end

利用UICollectionView实现瀑布流的更多相关文章

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

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

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

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

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

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

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

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

  5. OC-UICollectionView实现瀑布流

    UICollectionView实现瀑布流 在iOS中可以实现瀑布流的目前已知的有2种方案: 使用UIScrollView自己封装一套,这种方案是应用于iOS6之前的,因为iOS6才出来UIColle ...

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

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

  7. IOS 瀑布流UICollectionView实现

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

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

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

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

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

随机推荐

  1. Linux cd命令

    1: cd  不加任何参数,它会自动跳到用户的家目录中去! 2: ~ 表示用户的家目录 3: cd ~userNmae/     这样可以进入指定用户的家目录中去! 4: cd - 跳到上一次所在的目 ...

  2. Mac使用技巧

    外接显示器的生活,在 系统偏好设置--显示器--排列中,点击那个白色的条,可以设置主显示器.

  3. Android 优化性能之 如何避免--过度绘制

    可能有些人不明白什么是过度绘制,简单言,我们app一个页面所显示的效果是由像素一帧一帧绘制而成.过度绘制就是意味着这一帧被绘制多次.如果是静态的布局,可能影响不是很大,如果是动态的,比如ListVie ...

  4. ExpandableListView(一)替换系统默认的箭头

    很多朋友可能在android开发中,用过ExpandableListView这个组件,这个组件功能强大,比传统的ListView有好多优势.然而在开发中,我相信有好多人,包括我个人都会遇到下面的一些问 ...

  5. MyMVC框架的使用

    1)在web.config 中system.web 节点下加入例如以下代码 <pages controlRenderingCompatibilityVersion="4.0" ...

  6. nginx与ios实现https双向认证

    服务端配置 nginx关键配置例如以下: listen 443; server_name localhost; ssl on; ssl_certificate /usr/local/opt/nginx ...

  7. OOP中的多态

    尽管一直在说OOP,但说实话还不是真正的理解,面向对象的三个基本特性继承.封装.多态,前两个性质曾经 有接触听的比較多还好理解,以下主要介绍一下第三个特性--多态. 1. 定义     同一操作作用于 ...

  8. java进制转换器 图形用户界面 十进制及其相反数分别转化为二,四,八,十六进制

    package com.rgy.Test; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; ...

  9. Android 动画animation 深入分析

    转载请注明出处:http://blog.csdn.net/farmer_cc/article/details/18259117 Android 动画animation 深入分析 前言:本文试图通过分析 ...

  10. hdu acm 2154(多解取一解)

    //题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...