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 ...
随机推荐
- 免费版:Xshell和Xftp下载路径
家庭版Xshell和Xftp下载地址: 下载地址:https://www.netsarang.com/zh/free-for-home-school/
- Java:Apache Commons 工具类介绍及简单使用
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. Commons简介 组件 功能介绍 commo ...
- Python 绘制词云
文本内容:data(包含很多条文本) 1.分词: import jieba data_cut = data.apply(jieba.lcut) 2.去除停用词: stoplist.txt:链接:htt ...
- python自定义异常,使用raise引发异常
1.自定义异常类,自定义的异常类必须是Exception或者Error的子类! 1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 class Illega ...
- PHP实现的解汉诺塔问题算法示例
问题描述: 相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘(如下图).游戏的目标:把A杆 ...
- STM32中的通信协议
按照数据传送方式分: 串行通信(一条数据线.适合远距离传输)并行通信(多条数据线.成本高.抗干扰性差) 按照通信的数据同步方式分: 异步通信(以1个字符为1帧.发送与接收时钟不一致)同步通信(位同步. ...
- fasthttp:比net/http快十倍的Go框架(server 篇)
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/574 我们在上一篇文章中讲解了 Go HTTP 标准库的实现原理,这 ...
- java面向对象程序设计(下)-枚举类
在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象;再比如行星类,目前只有8个对象,这些实例有限而且固定的类,在Java中被称为枚举类 JDK1.5新增了一个enum关键字,(它与 ...
- 传统.NET 4.x应用容器化体验(5)
前面几篇都是基于阿里云ECS直接玩的,有童鞋问直接用Windows Server 2019可以玩不,本篇就为你介绍一下如何给Windows Server 2019配置Docker环境. 1 准备工作 ...
- 动态 DP
一道入门 DP + 修改 = 动态 DP. 以模板题为例,多次询问树的最大独立集,带修改. 先有 naive 的 DP,记 \(f_{u,0/1}\) 表示 \(u\) 点不选/选时以 \(u\) 为 ...