用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.获取到数据 ...
随机推荐
- 使用fastjson进行转换
package junit.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; imp ...
- 1-hadoop中遇到的各种异常
本贴记录学习hadoop中遇到的各种异常, 包括推荐系统分类下的和本分类下的, 持续更新 1, 搭建ha时, active和standy之间不能自由切换 经检查, 配置文件错误, 私钥配置 root ...
- Behave用户自定义数据类型
在step句子中, 所有的参数默认是string类型, 如果用户想使用复杂的或者其他数据类型, 就需要了解以下bahave中的数据类型. behave的数据类型转换器是在parse和cfparse中支 ...
- UIKit 框架之UISearchBar、UITableViewController
// // tableViewController.m // tableVC // // Created by City--Online on 15/6/1. // Copyright (c) 201 ...
- Spring学习之路
(一)搭建Spring.NET环境常见的DLL 1.spring.core --整个框架的基础,实现了依赖注入的功能 2.Spring.AOP--提供面向方面编程(aop)的支持 3.Spring.D ...
- Jquery链式编程及Index()详解
Jquery中的方法基本 上都可以返回一个Jquery对象, 如: <body> <div class="divcontent"> <p>中国& ...
- StreamRead和StreamWriter的使用
//StreamRead来读取一个文件 using (StreamReader sr = new StreamReader(@"C:\Users\enle\Desktop\新建文本文档.tx ...
- sql基础语法大全 转载过来的,出处忘了!
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- 啰里吧嗦redis
1.redis是什么 redis官网地址 Redis is an open source (BSD licensed), in-memory data structure store, used as ...
- java 获取控制台输入
读取控制台输入 从控制台读取一行数据,返回值字符串 public class IO { public static void main(String args[]) throws IOExceptio ...