用CHTCollectionViewWaterfallLayout写瀑布流
用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写瀑布流的更多相关文章
- bootstrap+masonry.js写瀑布流
最近在用bootstrap写一个网站,其中有个图文展示的页面要用到瀑布流的效果.因为项目要求,项目要以bootstrap为基准,不准私自添加内联样式.内部样式,所以,自己写瀑布流就不行了,所以,根据要 ...
- 分享:纯 css 瀑布流 和 js 瀑布流
分享一次纯 css 瀑布流 和 js 瀑布流 纯 css 写瀑布流 1.multi-columns 方式: 通过 Multi-columns 相关的属性 column-count.column-ga ...
- 关于瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)
瀑布流 又称瀑布流式布局,是比较流行的一种网站页面布局方式.即多行等宽元素排列,后面的元素依次添加到其后,等宽不等高,根据图片原比例缩放直至宽度达到我们的要求,依次按照规则放入指定位置. 为什么使用瀑 ...
- UICollectionView 很简单的写个瀑布流
你项目中要用到它吗? 可能会在你的项目中用到这玩意,最近也是要用就简单的写了一个 Demo.没多少代码,就不放Git了,下面会详细点的说说代码的,要还有什么问题的小伙伴可以直接Q我,也可以把Demo发 ...
- 用vue.js写的一个瀑布流的组件
用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402
- 瀑布流封装(仿写UITableView)
本篇文章将会仿照苹果系统提供的UITableView类,封装一个瀑布流效果的控件!!! 该控件和系统的UITableView是相同级别的 (继承自系统的UIScrollView) GitHub中Dem ...
- jquery瀑布流的制作
首先,还是来看一下炫酷的页面: 今天就边做边说了: 一.准备工作 新建css,js,img文件夹存放相应文件,并在demo.html文件中引入外部文件(注意要把jquery文件引入),这里就不过多描述 ...
- CollectionView水平和竖直瀑布流的实现
最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...
- 用jquery实现瀑布流案例
一.瀑布流是我们常见的案例,这里主要讲述,用jquery的方式实现瀑布流的功能! 引言:我们经常见到很多网站的瀑布流功能,如淘宝.京东这些商品等等.. 实现它我们首先考虑几个问题:1.获取到数据 ...
随机推荐
- Etcd安全配置之Basic Auth认证
<中小团队落地配置中心详解>文章中我们介绍了如何基于Etcd+Confd构建配置中心,最后提到Etcd的安全问题时说了可以使用账号密码认证以达到安全访问的目的,究竟该如何开启认证以及怎么设 ...
- java aop做一个接口耗时的计算
看代码: @Aspect @Component public class TimeCostAspect { private static Logger logger = LoggerFactory.g ...
- ConfigurationManager
ConfigurationManager读取和写入 提供对客户端应用程序配置文件的访问 通过引入System.Configuration.dll可以用ConfigurationManager类来读取项 ...
- rails中常用的插件
config.gem "acts-as-taggable-on", :version => '1.0.19' # tag类 config.gem "papercli ...
- Spring IOC(DI)
软件152 余建强 1 什么是IOC IOC—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不 ...
- 阿里巴巴Java开发手册(详尽版)-个人未注意到的知识点(转)
转自 https://blog.csdn.net/u013039395/article/details/86528164 一.编程规约 (一) 命名风格 [强制]代码中的命名只可用英文方式 [强制]类 ...
- Shell脚本编写2------有关变量
shell脚本中变量定义方式十分简单,直接将值赋值给变量较好例如 :name="tuanzhang"注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.变量命名 ...
- 实现Vue 的 markdown 文档可以在线运行(vue-markdown-run)
闲暇时间我用Vue框架写了一个博客,编辑器是用的markdown文本的形式,介绍性+描述完全能满足我的需求,但是,如果想在线运行我markdown文本中的Vue组件代码,则无法实现了, 于是我就自己写 ...
- Autofac--手动依赖注入
本文只适合初步接触 autoafc 的小白,大佬勿喷. autofac是一种很轻量的一个依赖注入容器 暂时先说一下手动注入的方式(因为使用自动注入遇到了坑,暂时先写一下手动注入) 1.先定义接口 pu ...
- 前端模块化(AMD和CMD、CommonJs)
知识点1:AMD/CMD/CommonJs是JS模块化开发的标准,目前对应的实现是RequireJs/SeaJs/nodeJs. 知识点2:CommonJs主要针对服务端,AMD/CMD主要针对浏览器 ...