前言

最近项目中用到了类似蜂窝的六边形布局,在这里分享出来抛砖引玉,供大家参考学习。本文提供了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的更多相关文章

  1. iOS开发——MVC详解&Swift+OC

    MVC 设计模式 这两天认真研究了一下MVC设计模式,在iOS开发中这个算是重点中的重点了,如果对MVC模式不理解或者说不会用,那么你iOS肯定学不好,或者写不出好的东西,当然本人目前也在学习中,不过 ...

  2. IOS开发之绝对布局和相对布局(屏幕适配)

    之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...

  3. ios开发——实用技术OC-Swift篇&触摸与手势识别

    iOS开发学习之触摸事件和手势识别   iOS的输入事件 触摸事件 手势识别 手机摇晃 一.iOS的输入事件   触摸事件(滑动.点击) 运动事件(摇一摇.手机倾斜.行走),不需要人为参与的 远程控制 ...

  4. ios开发——实用技术OC-Swift篇&本地通知与远程通知详解

    本地通知与远程通知详解 一:本地通知   Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...

  5. 【iOS开发】如何用 Swift 语言进行LBS应用的开发?

    本文分为三部分,第一部分详解用Swift语言开发LBS应用,并给出完整的示例与源代码:第二部分介绍如何申请LBS密钥,第三部分是综合示例查看,扫描二维码即可查看示例demo. 第一部分 使用Swift ...

  6. iOS开发~UI布局(三)深入理解autolayout

    一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...

  7. iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解

    一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.a ...

  8. iOS开发~UI布局(一)初探Size Class

    随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...

  9. iOS开发之-- oc 和 swift混编之自建桥接文件

    进行swift开发的时候,oc 的项目已经进行了很长一段时间,所以默认使用Xcode自建的桥接文件的时候,这个桥接文件名称是固定的,放置的目录也是无法更改的,所以我就想自己创建一个桥接文件,然后在ta ...

随机推荐

  1. redis--hash的实现

    Redis数据结构---字典,哈希表,dict 或java中的map,数据使用key -> value的形式存储,整个redis数据库就是基于字典实现,api见hash REDIS的hash实现 ...

  2. SpringMVC(5)数据绑定-2

    在SpringMVC(4)数据绑定-1中我们介绍了如何用@RequestParam来绑定数据,下面我们来看一下其它几个数据绑定注解的使用方法. 1.@PathVariable 用来绑定URL模板变量值 ...

  3. oracle 大表在线删除列操作(alter table table_name set unused )

    在某些情况下业务建的表某些列没有用到,需要进行删除,但是如果是数据量很大的大表,直接 alter table table_name drop column column_name;这种方法删除,那么将 ...

  4. 谷粒商城--分布式基础篇(P1~P27)

    谷粒商城--分布式基础篇P1~P27 去年3月份谷粒商城分布式基础.进阶.高级刚出的时候就开始学了,但是中途因为一些事就中断了,结果一直到现在才有时间重新开始学,看到现在网上这么多人都学完了,确实感觉 ...

  5. win10 IIS web.config加密不能访问:打不开 RSA 密钥容器

    C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 找到密钥文件, 根据时间判断具体是哪一个文件,赋予network service读权限

  6. Java之注解与反射

    Java之注解与反射 注解(Annotation)简介 注解(Annotation)是从JDK5.0引入的新技术 Annotation作用:注解(Annotation)可以被其他程序如编译器等读取 A ...

  7. final修饰符(3)-基本类型变量和引用类型变量的区别

    final修饰基本类型变量 当使用final修饰基本类型变量时,不能对基本类型变量重新赋值,因此基本类型变量不能被改变 final修饰引用类型变量 当使用final修饰引用类型变量时,它保存的仅仅是一 ...

  8. Day11继承、封装、多态-面向对象编程(2)

    封装 我们设计程序要追求 高内聚,低耦合 . 高内聚:类的内部数据操作细节自己完成,不允许外部干涉 低耦合:仅暴露少量方法给外部使用 封装(数据的隐藏) 通常,应禁止直接访问一个对象中数据的实际表示, ...

  9. 微信小程序云开发-列表下拉刷新

    一.json文件开启页面刷新 开启页面刷新.在页面的json文件里配置两处: "enablePullDownRefresh": true, //true代表开启页面下拉刷新 &qu ...

  10. ZYNQ Linux 移植:包含petalinux移植和手动移植debian9

    参考: https://electronut.in/workflow-for-using-linux-on-xilinx-zynq/ https://blog.csdn.net/m0_37545528 ...