Collection view自定义布局

一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法:

  • prepareLayout():当准备开始布局时调用这个方法,可以在这里计算一些属性,比如cell的尺寸。
  • layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]?:在这里返回布局属性。

实例(比较简单的例子,实际开发中可以进行更多的封装)

第一步:声明两个属性

    // MARK: 属性
/** cell的个数 */
var itemCount: Int? /** 布局信息 */
var layoutAttributes = [UICollectionViewLayoutAttributes]()

第二步:重写prepareLayout方法,计算布局属性

    // MARK: 准备布局
override func prepareLayout() {
super.prepareLayout() // 计算每个cell的宽度
let width = (UIScreen.mainScreen().bounds.size.width - self.sectionInset.left - self.sectionInset.right - minimumInteritemSpacing) / 2.0 // 判断itemCount是否为空
if itemCount != nil {
// 开始计算每个cell的布局属性
calculationAttribute(withItemWidth: width)
}
}
func calculationAttribute(withItemWidth itemWidth: CGFloat) {
// 声明数组 保存每一列的总高度 (两列)
let column1 = sectionInset.top, column2 = sectionInset.top
var columnArray: [CGFloat] = [column1, column2] for index in 0..<itemCount! {
// 创建indexPath 因为是示例 所以排版固定在一组
let indexPath = NSIndexPath(forItem: index, inSection: 0)
// 创建布局属性 通过indexPath
let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
// 创建一个随机高度
let itemHeight = CGFloat(arc4random()%150 + 100) // columnNumber 记录定义的是哪一列
var columnNumber: CGFloat
if columnArray[0] < columnArray[1] {
columnArray[0] += itemHeight + self.minimumLineSpacing
columnNumber = 0
} else {
columnArray[1] += itemHeight + self.minimumLineSpacing
columnNumber = 1
} // 设置item的位置
let x = self.sectionInset.left + (self.minimumLineSpacing + itemWidth) * columnNumber
let y = columnArray[Int(columnNumber)] - itemHeight - self.minimumLineSpacing
attribute.frame = CGRectMake(x, y, itemWidth, itemHeight) layoutAttributes.append(attribute)
}
// 当设置完所有item的位置后需要设置itemsize 这样才能保证滚动时能显示所有的cell
// itemsize的height属性 是取最高的那列的平均值
if columnArray[0] > columnArray[1] {
self.itemSize = CGSizeMake(itemWidth, (columnArray[0] - sectionInset.top) * 2 / CGFloat(itemCount!) - self.minimumLineSpacing)
} else {
self.itemSize = CGSizeMake(itemWidth, (columnArray[1] - sectionInset.top) * 2 / CGFloat(itemCount!) - self.minimumLineSpacing)
}
}

第三步:返回布局信息数组

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return layoutAttributes
}

总结:这个例子是瀑布流的布局,如果需要别的布局需要在准备布局时计算,然后返回布局信息数组就可以了

Collection View 自定义布局(custom flow layout)的更多相关文章

  1. 自定义 Collection View 布局

    自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...

  2. iOS系列译文:自定义Collection View布局

    原文出处: Ole Begemann   译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...

  3. Collection View Programming Guide for iOS---(四)---Using the Flow Layout

      Using the Flow Layout使用流布局 The UICollectionViewFlowLayout class is a concrete layout object that y ...

  4. 自定义Collection View布局

    转自answer-huang的博客 原文出自:Custom Collection View Layouts    UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...

  5. IOS UIView 03- 自定义 Collection View 布局

    注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...

  6. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  7. Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate

      Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...

  8. Collection View Programming Guide for iOS---(二)----Collection View Basics

      Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...

  9. Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example

    Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...

随机推荐

  1. [Node] 逃离回调地狱

    逃离Node回调地狱 Background : 在Node中,函数的返回结果大多利用回调的方式处理.如简单的判断文件是否存在并读取内容: var fs = require('fs'); fs.exis ...

  2. RichTextBox选中文本时往自己的其他的位置实现拖拽

          private void Form1_Load(object sender, EventArgs e) { richTextBox1.AllowDrop = true; richTextB ...

  3. [rxjs] Throttled Buffering in RxJS (debounce)

    Capturing every event can get chatty. Batching events with a throttled buffer in RxJS lets you captu ...

  4. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  5. Linux 性能优化之 IO 子系统 系列 图

    http://blog.sina.com.cn/s/articlelist_1029388674_11_1.html Linux 性能优化之 IO 子系统(一) 本文介绍了对 Linux IO 子系统 ...

  6. centos 6.6编译安装nginx--来自阿里云帮助文档

    刚开始接触运维工作, 需要安装nginx,就在网上找了各种的方法, 结果都是大家抄来抄去,都不好用. 由于公司用的是阿里云的服务器, 所以在阿里云上找到了安装nginx的方法,现在摘抄下来,供大家借鉴 ...

  7. Java基础知识强化之IO流笔记28:BufferedOutputStream / BufferedInputStream(字节缓冲区流) 之BufferedOutputStream写出数据

    1. BufferedOutputStream / BufferedInputStream(字节缓冲区流)的概述 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非 ...

  8. Ext信息提示对话框

    Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象,用来生成各种风格的信息提示对话框,其实例对象可以通过Ext.MessageBox或Ext.Msg ...

  9. docker build lnmp(未完成。。。)

    docker pull centos # 拉取镜像到本地 docker run -i -t -p 8000:80 --name=centosDev centos cat /etc/redhat-rel ...

  10. HTML5 Canvas 2D绘图

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4851774. ...