多組(section)瀑布流 swift5

可以直接復(fù)制使用

Simulator Screen Shot - iPhone 11 Pro Max - 2019-11-07 at 11.36.50.png

import UIKit

@objc protocol LauViewFlowLayoutDelegate : NSObjectProtocol {
    // 返回高度
    func LausetCellNormalSize(in collectionView:UICollectionView) -> CGSize
    // 返回行數(shù)
    @objc optional func LauColumnCountInWaterFlowLayout(in collectionView: UICollectionView) -> Int
    // 返回列間距
    @objc optional func LauDefaultColumnMarginInWaterFlowLayout(in collectionView: UICollectionView) -> CGFloat
    // 返回行間距
    @objc optional func LauRowMarginInWaterFlowLayout(in collectionView: UICollectionView) -> CGFloat
    //內(nèi)邊距
    @objc optional func LauEdgeInsetsInWaterFlowLayout(in collectionView: UICollectionView) -> UIEdgeInsets
    //組數(shù)
    @objc optional func LauNumberOfSections(in collectionView: UICollectionView) -> Int
    //特殊的cell在哪個(gè)Indexpath
    @objc optional func LauSpecialCellIndexpath(in collectionView:UICollectionView) -> IndexPath
    //特殊的cell的對應(yīng)size
    @objc optional func LauSpecialCellSize(in collectionView:UICollectionView, numberOfItemsInSection section: Int) -> CGSize

}

class LauLayout: UICollectionViewLayout {
    weak var delegate:LauViewFlowLayoutDelegate?
    
    /** 默認(rèn)列數(shù) */

    private var DefaultColumnCount : Int = 5
    private var DefaultSize:CGSize = CGSize.init(width: 50, height: 50)
    private var SpecialSize:CGSize = CGSize.init(width: 50, height: 50)
    private var SpecialIndex:IndexPath = IndexPath.init(item: 0, section: 0) //這個(gè)后續(xù)再支持
    // 記錄 collectionView 的 content 可滑動的范圍
    fileprivate var contentScope: CGFloat = 0
    /** 每一列之間的間距 垂直 */
    private var DefaultColumnMargin : CGFloat = 0.0
    /** 每一行之間的間距 水平方向 */
    private var DefaultRowMargin : CGFloat = 0.0
    /** 邊緣間距 */
    private var DefaultEdgeInsets : UIEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    private var LastSectionMaxHeight:CGFloat = 0
    //懶加載
    //存放所有cell的布局屬性
    fileprivate lazy var attrsArrayArray = [UICollectionViewLayoutAttributes]()
    //存放所有列的當(dāng)前高度
    fileprivate lazy var columnHeightsAry = [CGFloat]()
    //    fileprivate lazy var SectionItemHeightArray: [CGFloat] = []
    
    //第一步 :初始化
    override func prepare() {
        super.prepare()
        //清除高度
        columnHeightsAry.removeAll()
        self.contentScope = 0.0
        self.getconfigData()
        for _ in 0 ..< DefaultColumnCount {
            columnHeightsAry.append(DefaultEdgeInsets.top)
        }
        
        //清除所有的布局屬性
        attrsArrayArray.removeAll()
        
        let sections : Int = (self.collectionView?.numberOfSections)!
        for num in 0 ..< sections {
            let count : Int = (self.collectionView?.numberOfItems(inSection: num))!//獲取分區(qū)0有多少個(gè)item
            if self.delegate != nil && self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauSpecialCellSize(in:numberOfItemsInSection:))) ?? false {
                if let contentView = self.collectionView {
                    self.SpecialSize =  self.delegate?.LauSpecialCellSize?(in: contentView, numberOfItemsInSection: num) ?? CGSize.init(width: self.DefaultSize.width, height: self.DefaultSize.height)
                    
                }
                
            }
            for i in 0 ..< count {
                let indexpath : NSIndexPath = NSIndexPath.init(item: i, section: num)
                let attrsArray = self.layoutAttributesForItem(at: indexpath as IndexPath)!
                attrsArrayArray.append(attrsArray)
            }
            let (_, maximumInteritemHeight) = maximumInteritemForSection(heightArray: columnHeightsAry)
            self.LastSectionMaxHeight = maximumInteritemHeight
            columnHeightsAry.removeAll()
            for _ in 0 ..< DefaultColumnCount {
                columnHeightsAry.append(DefaultEdgeInsets.top)
            }
        }
    }
    
    /**
     * 第二步 :返回indexPath位置cell對應(yīng)的布局屬性
     */
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attrsArray = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
        let _ItemWidth = DefaultSize.width
        var _ItemHeight = DefaultSize.height
        
        if self.SpecialSize.height != DefaultSize.height {
            if let nums = self.collectionView?.numberOfItems(inSection: indexPath.section){
                let mutiple = SpecialSize.height / DefaultSize.height
                if indexPath.row % nums == 0 {
                    _ItemHeight = SpecialSize.height + DefaultRowMargin * mutiple
                }else{
                    _ItemHeight = DefaultSize.height
                }
            }else{
                _ItemHeight = DefaultSize.height
            }
        }else{
            _ItemHeight = DefaultSize.height
        }
        
        

        let (minimumInteritemNumber, minimumInteritemHeight) = minimumInteritemForSection(heightArray: columnHeightsAry)
        let itemX = DefaultEdgeInsets.left + CGFloat(minimumInteritemNumber) * (_ItemWidth + DefaultColumnMargin)
        var itemY = minimumInteritemHeight
        let (_, maximumInteritemHeight) = maximumInteritemForSection(heightArray: columnHeightsAry)
        if indexPath.item < self.DefaultColumnCount {
            itemY = itemY + self.DefaultEdgeInsets.top + LastSectionMaxHeight
        }

        attrsArray.frame = CGRect(x: itemX, y: itemY, width: _ItemWidth, height: CGFloat(_ItemHeight))
        columnHeightsAry[minimumInteritemNumber] = attrsArray.frame.maxY
        if contentScope < maximumInteritemHeight {
            self.contentScope = maximumInteritemHeight
            self.contentScope = contentScope + DefaultEdgeInsets.bottom
        }
        return attrsArray
    }
    
    //第三步 :重寫  返回所有列的高度
    override var collectionViewContentSize: CGSize {
        return CGSize.init(width: 0, height: self.contentScope + DefaultEdgeInsets.bottom)
    }
    
    //第四步 :返回collection的item的frame
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        return attrsArrayArray
    }
    
    private func getconfigData()  {
        guard let contentView = self.collectionView else { return }
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LausetCellNormalSize(in:))) ?? false) {
            self.DefaultSize = self.delegate?.LausetCellNormalSize(in: contentView) ?? CGSize.init(width: 50, height: 50)
        }
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauRowMarginInWaterFlowLayout(in:))) ?? false){
            self.DefaultColumnCount = self.delegate?.LauColumnCountInWaterFlowLayout?(in: contentView) ?? 0
        }
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauDefaultColumnMarginInWaterFlowLayout(in:))) ?? false) {
            self.DefaultRowMargin = self.delegate?.LauDefaultColumnMarginInWaterFlowLayout?(in: contentView) ?? 0
        }
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauRowMarginInWaterFlowLayout(in:))) ?? false) {
            self.DefaultRowMargin = self.delegate?.LauRowMarginInWaterFlowLayout?(in: contentView) ?? 0
        }
        
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauEdgeInsetsInWaterFlowLayout(in:))) ?? false) {
            self.DefaultEdgeInsets = self.delegate?.LauEdgeInsetsInWaterFlowLayout?(in: contentView) ?? UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        }
        if self.delegate != nil && (self.delegate?.responds(to: #selector(LauViewFlowLayoutDelegate.LauSpecialCellIndexpath(in:))) ?? false ) {
            self.SpecialIndex = self.delegate?.LauSpecialCellIndexpath?(in: contentView) ?? IndexPath.init(item: 0, section: 0)
        }
    }
    
    /*
     *  豎向布局: 計(jì)算高度最大的是哪一列                 / 橫向布局:計(jì)算寬度最大的是哪一行
     *
     *  @param  heightArray: 緩存 section 高度的數(shù)組  / 緩存 section 寬度的數(shù)組
     *  return  返回最大列的列號和高度值                / 返回最大行的行號和寬度值
     */
    fileprivate func maximumInteritemForSection(heightArray: [CGFloat]) -> (Int, CGFloat) {
        if heightArray.count <= 0 {
            return (0, 0.0)
        }
        // 默認(rèn)第0列的高度最小
        var maximumInteritemNumber = 0
        // 從緩存高度數(shù)組中取出第一個(gè)元素,作為最小的那一列的高度
        var maximumInteritemHeight = heightArray[0]
        // 遍歷數(shù)組,查找出最小的列號和最小列的高度值
        for i in 1..<heightArray.count {
            let tempMaximumInteritemHeight = heightArray[i]
            if maximumInteritemHeight < tempMaximumInteritemHeight {
                maximumInteritemHeight = tempMaximumInteritemHeight
                maximumInteritemNumber = i
            }
        }
        return (maximumInteritemNumber, maximumInteritemHeight)
    }
    /*
     *  豎向布局: 計(jì)算高度最小的是哪一列                 / 橫向布局:計(jì)算寬度最小的是哪一行
     *
     *  @param  heightArray: 緩存 section 高度的數(shù)組  / 緩存 section 寬度的數(shù)組
     *  return  返回最小列的列號和高度值                / 返回最小行的行號和高度值
     */
    fileprivate func minimumInteritemForSection(heightArray: [CGFloat]) -> (Int, CGFloat) {
        if heightArray.count <= 0 {
            return (0, 0.0)
        }
        // 默認(rèn)第0列的高度最小
        var minimumInteritemNumber = 0
        // 從緩存高度數(shù)組中取出第一個(gè)元素,作為最小的那一列的高度
        var minimumInteritemHeight = heightArray[0]
        // 遍歷數(shù)組,查找出最小的列號和最小列的高度值
        for i in 1..<heightArray.count {
            let tempMinimumInteritemHeight = heightArray[i]
            if minimumInteritemHeight > tempMinimumInteritemHeight {
                minimumInteritemHeight = tempMinimumInteritemHeight
                minimumInteritemNumber = i
            }
        }
        return (minimumInteritemNumber, minimumInteritemHeight)
    }
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容