iOS自定义UICollectionViewLayout实现瀑布流布局

所属分类: 软件编程 / IOS 阅读数: 795
收藏 0 赞 0 分享

移动端访问不佳,请访问我的个人博客

最近项目中需要用到瀑布流的效果,但是用UICollectionViewFlowLayout又达不到效果,自己动手写了一个瀑布流的layout,下面是我的心路路程
先上效果图与demo地址

因为是用UICollectionView来实现瀑布流的,决定继承UICollectionViewLayout来自定义一个layout来实现一个简单瀑布流的布局,下面是需要重写的方法:

重写这个属性得出UICollectionView的ContentSize:collectionViewContentSize
重写这个方法来得到每个item的布局:layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
重写这个方法给UICollectionView所有item的布局:layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
重写这个方法来实现UICollectionView前的操作:prepare()

实现思路

通过代理模式获得到需要的列数和每一item的高度,用过列数与列之间的间隔和UICollectionView的宽度来得出每一列的宽度,item从左边到右布局,下一列的item放到高度最小的列下面,防止每列的高度不均匀,下面贴上代码和注释:

import UIKit

@objc protocol WCLWaterFallLayoutDelegate {
 //waterFall的列数
 func columnOfWaterFall(_ collectionView: UICollectionView) -> Int
 //每个item的高度
 func waterFall(_ collectionView: UICollectionView, layout waterFallLayout: WCLWaterFallLayout, heightForItemAt indexPath: IndexPath) -> CGFloat
}

class WCLWaterFallLayout: UICollectionViewLayout {

 //代理
 weak var delegate: WCLWaterFallLayoutDelegate?
 //行间距
 @IBInspectable var lineSpacing: CGFloat = 0
 //列间距
 @IBInspectable var columnSpacing: CGFloat = 0
 //section的top
 @IBInspectable var sectionTop: CGFloat = 0 {
 willSet {
  sectionInsets.top = newValue
 }
 }
 //section的Bottom
 @IBInspectable var sectionBottom: CGFloat = 0 {
 willSet {
  sectionInsets.bottom = newValue
 }
 }
 //section的left
 @IBInspectable var sectionLeft: CGFloat = 0 {
 willSet {
  sectionInsets.left = newValue
 }
 }
 //section的right
 @IBInspectable var sectionRight: CGFloat = 0 {
 willSet {
  sectionInsets.right = newValue
 }
 }
 //section的Insets
 @IBInspectable var sectionInsets: UIEdgeInsets = UIEdgeInsets.zero
 //每行对应的高度
 private var columnHeights: [Int: CGFloat]   = [Int: CGFloat]()
 private var attributes: [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()

 //MARK: Initial Methods
 init(lineSpacing: CGFloat, columnSpacing: CGFloat, sectionInsets: UIEdgeInsets) {
 super.init()
 self.lineSpacing = lineSpacing
 self.columnSpacing = columnSpacing
 self.sectionInsets = sectionInsets
 }

 required init?(coder aDecoder: NSCoder) {
 super.init(coder: aDecoder)
 }

 //MARK: Public Methods


 //MARK: Override
 override var collectionViewContentSize: CGSize {
 var maxHeight: CGFloat = 0
 for height in columnHeights.values {
  if height > maxHeight {
  maxHeight = height
  }
 }
 return CGSize.init(width: collectionView?.frame.width ?? 0, height: maxHeight + sectionInsets.bottom)
 }

 override func prepare() {
 super.prepare()
 guard collectionView != nil else {
  return
 }
 if let columnCount = delegate?.columnOfWaterFall(collectionView!) {
  for i in 0..<columnCount {
  columnHeights[i] = sectionInsets.top
  }
 }
 let itemCount = collectionView!.numberOfItems(inSection: 0)
 attributes.removeAll()
 for i in 0..<itemCount {
  if let att = layoutAttributesForItem(at: IndexPath.init(row: i, section: 0)) {
  attributes.append(att)
  }
 }
 }

 override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
 if let collectionView = collectionView {
  //根据indexPath获取item的attributes
  let att = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
  //获取collectionView的宽度
  let width = collectionView.frame.width
  if let columnCount = delegate?.columnOfWaterFall(collectionView) {
  guard columnCount > 0 else {
   return nil
  }
  //item的宽度 = (collectionView的宽度 - 内边距与列间距) / 列数
  let totalWidth = (width - sectionInsets.left - sectionInsets.right - (CGFloat(columnCount) - 1) * columnSpacing)
  let itemWidth = totalWidth / CGFloat(columnCount)
  //获取item的高度,由外界计算得到
  let itemHeight = delegate?.waterFall(collectionView, layout: self, heightForItemAt: indexPath) ?? 0
  //找出最短的那一列
  var minIndex = 0
  for column in columnHeights {
   if column.value < columnHeights[minIndex] ?? 0 {
   minIndex = column.key
   }
  }
  //根据最短列的列数计算item的x值
  let itemX = sectionInsets.left + (columnSpacing + itemWidth) * CGFloat(minIndex)
  //item的y值 = 最短列的最大y值 + 行间距
  let itemY = (columnHeights[minIndex] ?? 0) + lineSpacing
  //设置attributes的frame
  att.frame = CGRect.init(x: itemX, y: itemY, width: itemWidth, height: itemHeight)
  //更新字典中的最大y值
  columnHeights[minIndex] = att.frame.maxY
  }
  return att
 }
 return nil
 }

 override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
 return attributes
 }
}

最后附带demo地址,大家喜欢的话可以star一下

上面是简单的瀑布流的实现过程,希望大家能学到东西,有很多地方考虑的不足,欢迎大家交流学习,谢谢大家的阅读。

更多精彩内容其他人还在看

iOS逆向教程之跟踪函数调用详解

这篇文章主要给大家介绍了关于iOS逆向教程之跟踪函数调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

iOS App连续闪退时上报crash日志的方法详解

iOS App 有时可能遇到启动必 crash 的绝境:每次打开 App 都闪退,无法正常使用App。下面这篇文章主要给大家介绍了iOS App连续闪退时上报crash日志的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键详解

xcode是苹果公司向开发人员提供的集成开发环境,开发者们经常会使用到,下面这篇文章主要给大家介绍了关于如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS指纹登录(TouchID)集成方案详解

这篇文章主要为大家详细介绍了iOS指纹登录TouchID的集成方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS动态验证码实现代码

本文通过实例代码给大家介绍了ios动态验证码的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

iOS模块化开发浅析

本文给大家分析了IOS在模块发开发时候的相关注意点以及简单代码做了分享,有兴趣的朋友参考学习下。
收藏 0 赞 0 分享

iOS中封装.framework及使用的方法详解

这篇文章主要给大家介绍了关于iOS中封装.framework及使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
收藏 0 赞 0 分享

WKWebView、WebView和JS的交互方式详解

这篇文章主要给大家介绍了关于WKWebView、WebView和JS的交互方式,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

ios wkwebview离线化加载h5资源解决方案

本篇文章主要介绍了ios wkwebview离线化加载h5资源解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS实现带指引线的饼状图效果(不会重叠)

饼状图对大家来说应该都不陌生,下面这篇文章主要给大家介绍了关于iOS实现带指引线的饼状图效果(不会重叠)的相关资料,文章通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享
查看更多