Collection View 自定义布局(custom flow layout)
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)的更多相关文章
- 自定义 Collection View 布局
自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...
- iOS系列译文:自定义Collection View布局
原文出处: Ole Begemann 译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...
- Collection View Programming Guide for iOS---(四)---Using the Flow Layout
Using the Flow Layout使用流布局 The UICollectionViewFlowLayout class is a concrete layout object that y ...
- 自定义Collection View布局
转自answer-huang的博客 原文出自:Custom Collection View Layouts UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...
- IOS UIView 03- 自定义 Collection View 布局
注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...
- Collection View Programming Guide for iOS---(六)---Creating Custom Layouts
Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...
- 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 ...
- Collection View Programming Guide for iOS---(二)----Collection View Basics
Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...
- 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.<%= %>只能得到当前面定义的值.即你在这个页里有<% int a=100%>--你在下面才可以使用<%=a%>2.${ }这个是el表达式.可以从上下文中得 ...
- 在Eclipse中怎样公布创建的JavaWebproject
博客<在Eclipse中怎样创建JavaWebproject>中图文并茂的说明了Eclipse中创建JavaWebproject的方法:博客<怎样为Eclipse开发工具中创建的Ja ...
- [Javascript] MetaProgramming: new.target
new.target is a new “magical” value available in all functions, thoughin normal functions it will al ...
- careercup-数组和字符串1.3
1.3 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排序后,能否变成另一个字符串. C++实现代码: #include<iostream> #include<map> ...
- LabVIEW设计模式系列——事件结构中值改变事件
标准:1.将具有值改变事件的控件,放置在其事件结构的值改变页面里.
- PureMVC(JS版)源码解析(五):SimpleCommand类
之前我们对PureMVC中涉及到观察者模式的三个基本类(Notification/Observer/Notifier)进行了分析,接下来将对PureMVC源码中的其他类进行分析,首先我们讲 ...
- iOS 关于开发者证书:此证书的签发者无效的解决方案
备注:第二个步骤一定要进行,否则弄到吐血,还是现实签发者无效 ---------------------- 1,按照你那个链接下载,https://developer.apple.com/certif ...
- 用php切割大图片为成规则的小图
将根据xml配置,将合并后的大图切割成一系列小图 <?php /** * 将大图片按照配置切割成一定比例的小图片 * 并按照一定规则给小图片命名 * * 使用方法: *根据guardians/g ...
- idea集成svn插件
1.需要在机器上安装一个SVN客户端命令行程序,可以到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows 我选择的是torto ...
- [DEncrypt] RSACryption--RSA加密/解密字符串 (转载)
点击下载 RSACryption.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.RSACryption RSA 的密钥产生2.RSACryption RSA的加密函数3.RSACrypt ...