iOS开发之蜂窝布局—Swift
前言
最近项目中用到了类似蜂窝的六边形布局,在这里分享出来抛砖引玉,供大家参考学习。本文提供了2种思路实现效果,第一种方式使用UICollectionView实现,第二种方式使用UIScrollView实现,两种方式底层核心思想是一致的。
效果图

一、UICollectionView
由于UICollectionView自身提供很多属性,所以只需要自定义UICollectionViewFlowLayout布局,内部计算每个控件的位置就可以很轻松的实现。
核心代码
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return }
scrollDirection = .vertical
attributesArray = nil
itemWidth = (collectionView.bounds.width - minimumInteritemSpacing * CGFloat(itemsPerRow - 1) - collectionView.contentInset.left - collectionView.contentInset.right) / CGFloat(itemsPerRow)
itemSideLength = itemWidth / sqrt(3)
itemHeight = itemSideLength * 2
itemSize = CGSize(width: itemWidth, height: itemHeight)
heightOfGroup = itemSideLength + itemSize.height + 2 * minimumLineSpacing
itemsPerGroup = itemsPerRow + itemsPerRow - 1
items = collectionView.numberOfItems(inSection: 0)
contentSize = {
let group = CGFloat(items / itemsPerGroup)
let groupModulo = items % itemsPerGroup
let residualRow = (groupModulo <= (itemsPerRow - 1)) ? 1 : 2
let residualHeight: CGFloat = {
if groupModulo == 0 {
return itemHeight * 0.25
}else if residualRow == 2 {
return heightOfGroup + itemHeight * 0.25
}else {
return itemHeight
}
}()
return CGSize(width: collectionView.bounds.width - collectionView.contentInset.left - collectionView.contentInset.right, height: group * heightOfGroup + residualHeight)
}()
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
if let attributesArray = attributesArray {
return attributesArray
}else {
attributesArray = Array(0..<items).compactMap({layoutAttributesForItem(at: IndexPath(item: $0, section: 0))})
return attributesArray
}
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let groupIndex: Int = indexPath.row / itemsPerGroup
let indexInGroup: Int = indexPath.row % itemsPerGroup
let isFirstLine: Bool = indexInGroup < Int(itemsPerGroup / 2)
let indexInLine: Int = isFirstLine ? indexInGroup : indexInGroup - Int(itemsPerGroup / 2)
let x = (itemSize.width) * (CGFloat(indexInLine) + (isFirstLine ? 0.5 : 0)) + CGFloat(indexInLine) * minimumInteritemSpacing + (isFirstLine ? minimumInteritemSpacing * 0.5 : 0)
let y = (itemSize.height) * (isFirstLine ? 0 : 0.75) + heightOfGroup * CGFloat(groupIndex) + (isFirstLine ? 0 : minimumLineSpacing)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = CGRect(x: x, y: y, width: itemSize.width, height: itemSize.height)
return attributes
}
二、UIScrollView
UIScrollView实现相对比较复杂,内部主要涉及到控件的复用、位置计算、点击事件。
1.复用
考虑到大量数据源,我们内部需要一个复用机制来保障性能,参考UITableView的Cell复用原理和源码分析,自己在内部完成一个复用池。
复用池核心代码
func pushCell(_ cell: CLHoneycombCell, forReuseIdentifier identifier: String) {
semaphore.wait()
defer {
semaphore.signal()
}
if cacheCells[identifier] == nil {
cacheCells[identifier] = []
}
cacheCells[identifier]?.add(cell)
}
func popCell(forReuseIdentifier identifier: String) -> CLHoneycombCell? {
semaphore.wait()
defer {
semaphore.signal()
}
if let cell = cacheCells[identifier]?.anyObject() as? CLHoneycombCell {
cacheCells[identifier]?.remove(cell)
return cell
}
return nil
}
func removeAll() {
semaphore.wait()
defer {
semaphore.signal()
}
cacheCells.removeAll()
}
2.位置计算
根据蜂窝布局特性,将控件进行分组,然后计算每一组中的每一个控件位置。滑动的时候需要先计算出新出现的控件,对其布局进行修正,然后需要找出滑出屏幕的控件,将其加入到复用池中。
核心代码
func invalidateLayout() {
guard let delegation = delegate, let dataSource = dataSource else { return }
cellRects.filter({displayingContentRect.containsVisibleRect($0.1) && visibleCells[$0.0] == nil}).forEach { (i, cellRect) in
let cell = dataSource.honeycombView(self, cellForRowAtIndex: i)
cell.frame = cellRect
delegation.honeycombView(self, willDisplayCell: cell, forIndex: i)
contentView.addSubview(cell)
visibleCells[i] = cell
}
visibleCells.filter({!displayingContentRect.containsVisibleRect(cellRects[$0.0] ?? .zero)}).forEach { (index, cell) in
cell.removeFromSuperview()
cell.setHighlighted(false)
cell.setSelected(false)
visibleCells[index] = nil
delegation.honeycombView(self, didEndDisplayingCell: cell, forIndex: index)
reusePool.pushCell(cell, forReuseIdentifier: cell.identifier)
}
}
3.点击事件
通过点击的位置在可见的控件数组中找出对应的控件索引,然后处理后续的事件。
核心代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
guard let touchPoint = touches.first?.location(in: contentView) else { return }
let containingRects = visibleCellRects.filter({$0.1.contains(touchPoint)})
if containingRects.count >= 2 {
var nearestIndexRect = containingRects.first!
for currentIndexRect in containingRects where distanceBetween(centerForRect(currentIndexRect.1), touchPoint) < distanceBetween(centerForRect(nearestIndexRect.1), touchPoint) {
nearestIndexRect = currentIndexRect
}
let indexForHighlight = nearestIndexRect.0
let explicit = delegate?.honeycombView(self, shouldHightlightItemAtIndex: indexForHighlight) ?? true
highlightItemAtIndex(indexForHighlight, explicit: explicit)
}else if containingRects.count == 1 {
let indexForHighlight = containingRects.first!.0
let explicit = delegate?.honeycombView(self, shouldHightlightItemAtIndex: indexForHighlight) ?? true
highlightItemAtIndex(indexForHighlight, explicit: explicit)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
let index = currentHighlightedIndex
guard index >= 0, let honeycomDelegate = delegate else { return }
unhighlightItemAtIndex(index)
let isSelected = visibleCells[index]?.isSelected ?? false
if isSelected, honeycomDelegate.honeycombView(self, shouldDeselectItemAtIndex: index) {
deselectItemAtIndex(index)
}else if !isSelected, honeycomDelegate.honeycombView(self, shouldSelectItemAtIndex: index) {
selectItemAtIndex(index)
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
guard currentHighlightedIndex >= 0 else { return }
unhighlightItemAtIndex(currentHighlightedIndex)
}
4.内部细节
UIScrollView实现其实就是相当于自己写了一个UICollectionView,内部思想基本上差不多,只是通过自己实现能够更好的自定义。其中还是有很多细节可以借鉴,这里为了保障自己的代理和滑动视图的代理不冲突,内部增加了一层contentView。
总结
核心代码已经贴出,完整代码请查看----->>>CLDemo,如果对你有所帮助,欢迎Star。
iOS开发之蜂窝布局—Swift的更多相关文章
- iOS开发——MVC详解&Swift+OC
MVC 设计模式 这两天认真研究了一下MVC设计模式,在iOS开发中这个算是重点中的重点了,如果对MVC模式不理解或者说不会用,那么你iOS肯定学不好,或者写不出好的东西,当然本人目前也在学习中,不过 ...
- IOS开发之绝对布局和相对布局(屏幕适配)
之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...
- ios开发——实用技术OC-Swift篇&触摸与手势识别
iOS开发学习之触摸事件和手势识别 iOS的输入事件 触摸事件 手势识别 手机摇晃 一.iOS的输入事件 触摸事件(滑动.点击) 运动事件(摇一摇.手机倾斜.行走),不需要人为参与的 远程控制 ...
- ios开发——实用技术OC-Swift篇&本地通知与远程通知详解
本地通知与远程通知详解 一:本地通知 Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...
- 【iOS开发】如何用 Swift 语言进行LBS应用的开发?
本文分为三部分,第一部分详解用Swift语言开发LBS应用,并给出完整的示例与源代码:第二部分介绍如何申请LBS密钥,第三部分是综合示例查看,扫描二维码即可查看示例demo. 第一部分 使用Swift ...
- iOS开发~UI布局(三)深入理解autolayout
一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...
- iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解
一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.a ...
- iOS开发~UI布局(一)初探Size Class
随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...
- iOS开发之-- oc 和 swift混编之自建桥接文件
进行swift开发的时候,oc 的项目已经进行了很长一段时间,所以默认使用Xcode自建的桥接文件的时候,这个桥接文件名称是固定的,放置的目录也是无法更改的,所以我就想自己创建一个桥接文件,然后在ta ...
随机推荐
- 其他:压力测试Jmeter工具使用
下载路径: http://yd01.siweidaoxiang.com:8070/jmeter_52z.com.zip 配置汉化中文: 找到jmeter的安装目录:打开 \bin\jmeter.pro ...
- SpringMvc实现批量删除,使用post传值一直报404错误
Ajax结合SpringMVC实现批量删除信息,在前台使用post向后台传递要删除的id的集合额时候,一直报404错误, 前台post传值的源码如下: 了解一下: (1)第二行的rows为前面得到的一 ...
- sqlplus 删除^H处理
1.在oracle用户下更改 2.在".profile"或者"~/.bash_profile"添加 stty erase ^H 3.wq,保存退出 stty时一 ...
- [网络流24题]最长k可重线段集[题解]
最长 \(k\) 可重线段集 题目大意 给定平面 \(x-O-y\) 上 \(n\) 个开线段组成的集合 \(I\) ,和一个正整数 \(k\) .试设计一个算法,从开线段集合 \(I\) 中选取开线 ...
- Linux groupadd and groupmod
groupadd [选项] group 三个参数: -g,--gid 指定组gid,除非使用-o,否则gid必须时唯一的 -o,--non-unique 允许创建有重复gid的组 -r, --syst ...
- C语言:宏参数的字符串化和宏参数的连接
在宏定义中,有时还会用到#和##两个符号,它们能够对宏参数进行操作. # 的用法 #用来将宏参数转换为字符串,也就是在宏参数的开头和末尾添加引号.例如有如下宏定义: #define STR(s) #s ...
- ES6新增语法(五)——Promise详解
Promise介绍 promise是一个对象,从它可以获取异步操作的消息.有all.race.reject.resolve这几个方法,原型上有then.catch等方法. Promise的两个特点: ...
- vue3 封装简单的 tabs 切换组件
背景:公司项目要求全部换成 vue3 ,而且也没有应用像 element-ui 一类的UI组件,用到的公共组件都是根据项目需求封装的,下面是使用vue3实现简单的tabs组件,我只是把代码分享出来,实 ...
- Java基础00-基础语法3
1. 注释 1.1 注释概述 1.2 注释分类 1.3 示例 2. 关键字 2.1 关键字概述 2.2 关键字的特点 3. 常量 3.1 常量的概述 3.2 常量分类 以上常量除了空常量都是可以直接输 ...
- 雪花算法(SnowFlake)Java实现
分布式id生成算法的有很多种,Twitter的SnowFlake就是其中经典的一种. 算法原理 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1bit,不用,因为二 ...