用CHTCollectionViewWaterfallLayout写瀑布流

实现的瀑布流效果图:

源码:

WaterfallCell.h 与 WaterfallCell.m

//
// WaterfallCell.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface WaterfallCell : UICollectionViewCell @property (nonatomic, strong) UIImageView *showImageView; @end
//
// WaterfallCell.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "WaterfallCell.h" @implementation WaterfallCell - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Scale the imageview to fit inside the contentView with the image centered:
CGRect imageViewFrame = CGRectMake(.f, .f,
CGRectGetMaxX(self.contentView.bounds),
CGRectGetMaxY(self.contentView.bounds));
_showImageView = [UIImageView new];
_showImageView.contentMode = UIViewContentModeScaleAspectFill;
_showImageView.frame = imageViewFrame;
_showImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_showImageView.clipsToBounds = YES;
[self addSubview:_showImageView];
self.layer.borderWidth = .f;
}
return self;
} @end

HeaderView.h 与 HeaderView.m

//
// HeaderView.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface HeaderView : UICollectionReusableView @end
//
// HeaderView.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "HeaderView.h" @implementation HeaderView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = .f;
}
return self;
} @end

FooterView.h 与 FooterView.m

//
// FooterView.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface FooterView : UICollectionReusableView @end
//
// FooterView.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "FooterView.h" @implementation FooterView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = .f;
}
return self;
} @end

使用时候的代码:

//
// RootViewController.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RootViewController.h"
#import "CHTCollectionViewWaterfallLayout.h" #import "WaterfallCell.h"
#import "HeaderView.h"
#import "FooterView.h" #define CELL_IDENTIFIER @"WaterfallCell"
#define HEADER_IDENTIFIER @"WaterfallHeader"
#define FOOTER_IDENTIFIER @"WaterfallFooter" @interface RootViewController ()<UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout> @property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *dataSourceSize; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 数据源
_dataSource = [NSMutableArray new];
for (int i = ; i <= ; i++)
{
[_dataSource addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]];
} // 初始化布局
CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init]; // 设置布局
layout.sectionInset = UIEdgeInsetsMake(, , , );
layout.headerHeight = ; // headerView高度
layout.footerHeight = ; // footerView高度
layout.columnCount = ; // 几列显示
layout.minimumColumnSpacing = ; // cell之间的水平间距
layout.minimumInteritemSpacing = ; // cell之间的垂直间距 // 初始化collectionView
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
collectionViewLayout:layout];
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor]; // 注册cell以及HeaderView,FooterView
[_collectionView registerClass:[WaterfallCell class]
forCellWithReuseIdentifier:CELL_IDENTIFIER];
[_collectionView registerClass:[HeaderView class]
forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
withReuseIdentifier:HEADER_IDENTIFIER];
[_collectionView registerClass:[FooterView class]
forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
withReuseIdentifier:FOOTER_IDENTIFIER]; // 添加到视图当中
[self.view addSubview:_collectionView];
} #pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"您点击了 %@", _dataSource[indexPath.row]);
} #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// 数据源
return [_dataSource count];
} - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
// 1个区
return ;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 注册collectionViewCell
WaterfallCell *cell = \
(WaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
forIndexPath:indexPath]; UIImage *image = _dataSource[indexPath.row];
cell.showImageView.image = image; return cell;
} - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil; if ([kind isEqualToString:CHTCollectionElementKindSectionHeader])
{
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:HEADER_IDENTIFIER
forIndexPath:indexPath];
} else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter])
{
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:FOOTER_IDENTIFIER
forIndexPath:indexPath];
} return reusableView;
} #pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 用以返回尺寸
UIImage *image = _dataSource[indexPath.row];
return image.size;
} @end

这个代码再怎么简单也会很复杂-_-!!

以下是使用的细节需要注意的地方:

设置的对应关系-

cell中的图片可不是随便设置的-

要注意返回每个cell的size值-

用CHTCollectionViewWaterfallLayout写瀑布流的更多相关文章

  1. bootstrap+masonry.js写瀑布流

    最近在用bootstrap写一个网站,其中有个图文展示的页面要用到瀑布流的效果.因为项目要求,项目要以bootstrap为基准,不准私自添加内联样式.内部样式,所以,自己写瀑布流就不行了,所以,根据要 ...

  2. 分享:纯 css 瀑布流 和 js 瀑布流

    分享一次纯 css 瀑布流  和 js 瀑布流 纯 css 写瀑布流 1.multi-columns 方式: 通过 Multi-columns 相关的属性 column-count.column-ga ...

  3. 关于瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)

    瀑布流 又称瀑布流式布局,是比较流行的一种网站页面布局方式.即多行等宽元素排列,后面的元素依次添加到其后,等宽不等高,根据图片原比例缩放直至宽度达到我们的要求,依次按照规则放入指定位置. 为什么使用瀑 ...

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

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

  5. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  6. 瀑布流封装(仿写UITableView)

    本篇文章将会仿照苹果系统提供的UITableView类,封装一个瀑布流效果的控件!!! 该控件和系统的UITableView是相同级别的 (继承自系统的UIScrollView) GitHub中Dem ...

  7. jquery瀑布流的制作

    首先,还是来看一下炫酷的页面: 今天就边做边说了: 一.准备工作 新建css,js,img文件夹存放相应文件,并在demo.html文件中引入外部文件(注意要把jquery文件引入),这里就不过多描述 ...

  8. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  9. 用jquery实现瀑布流案例

    一.瀑布流是我们常见的案例,这里主要讲述,用jquery的方式实现瀑布流的功能! 引言:我们经常见到很多网站的瀑布流功能,如淘宝.京东这些商品等等.. 实现它我们首先考虑几个问题:1.获取到数据   ...

随机推荐

  1. 数据库-SQLite简介

    SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库(轻型数据库). 由于资源占用少.性能良好和零管理成本,嵌入式数据库有了它的用武之地,像Android.iPhone都有内置的S ...

  2. 笔记四:python乱码深度剖析二

    一:学习内容 获取更改系统编码 判断字符的编码类型 文件存储和读取的编码 二:获取更改系统编码 1. 获取系统编码 import sys print sys.getdefaultencoding() ...

  3. Visual Studio最常用的快捷键

    Ctrl + J:快捷提示,强迫智能感知: Ctrl + 空格键:使用 IntelliSense(智能感知)自动完成: Ctrl + Z:撤销,回退键: Ctrl + Shift + 空格:强迫显示参 ...

  4. 《小岛经济学--鱼、美元和经济的故事》Digest

    作者:彼得.D.希夫(Peter D. Schiff)安德鲁.J.希(Andrew J. Schiff) How an Economy Grows and Why It Crashes 打车到清华,车 ...

  5. Oracle相关

    where 条件中使用=进行限制时,可以返回一个记录集,即可以返回多个记录集

  6. DataContractSerializer数据不一致下序列化

       一.数据类型的等效性 例如下面定义的两个类成员名称.定义顺序都不一样,但是在DataContract.DataMember的Name属性作用下,两个类的实例对象序列化后的xml是一样的,因此Or ...

  7. 第一次搭建dns服务器

    CentOS 7 搭建DNS服务器 主要参考的是小左先森的一篇博客:https://blog.51cto.com/13525470/2054121. 1.搭建过程中遇到的几个问题说一下: a.在重启服 ...

  8. 插入sql返回主键id

    <insert id="insertSelective" parameterType="com.xxx.model.XDetail" useGenerat ...

  9. 【学习笔记】--- 老男孩学Python,day10, 函数, 动态参数 命名空间\作用域 global nonlocal

    1. 动态参数 位置参数的动态参数: *args 关键字参数的动态参数 : **kwargs 顺序:位置---*args---默认值---**kwargs 在形参上*聚合, **聚合 在实参上*打散, ...

  10. Implementation:Bellman-ford

    单源最短路径算法Bellman-ford练习,可以处理有负边的情况(也可以在存在负圈时及时终止) #include <iostream> #include <cstdlib> ...