美文网首页
自定义collectionviewflowLayout

自定义collectionviewflowLayout

作者: 银月流苏 | 来源:发表于2021-05-13 15:44 被阅读0次
class CustomFlowLayout: UICollectionViewFlowLayout {
    var attributeArr: [UICollectionViewLayoutAttributes] = []
    var totalHeight: CGFloat = 0
    
    //insetArr(用来没放每组中的上线左右的距离)
    var insetDict: [Int: UIEdgeInsets] = [:]
    var minLineDict: [Int: CGFloat] = [:]
    var minInterDict: [Int: CGFloat] = [:]
    override func prepare() {
        super.prepare()
        guard let collectionView = self.collectionView else { return }
        let sections =  self.collectionView?.numberOfSections ?? 0
        ///左边距离的位置
        var leftWidth: CGFloat = 0
        var superWidth: CGFloat = collectionView.bounds.width
        var leaveWidth: CGFloat = superWidth
        for indexPathSection in 0..<sections {
            let numbersOfSection = self.collectionView?.numberOfItems(inSection: indexPathSection) ?? 0
            let inset = insetDict[indexPathSection] ?? UIEdgeInsets.zero
            let minInter = minInterDict[indexPathSection] ?? 0
            let minLine = minLineDict[indexPathSection] ?? 0


            //设置组头的布局
            if let headerAttribute = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath.init(item: 0,section: indexPathSection)) {
                headerAttribute.frame = CGRect.init(x: 0, y: totalHeight, width: headerAttribute.frame.width, height: headerAttribute.frame.height)
                totalHeight += inset.top + headerAttribute.frame.height
                attributeArr.append(headerAttribute)
            }
            
            for indexPathItem in 0..<numbersOfSection {
                let indexPath = IndexPath.init(item: indexPathItem, section: indexPathSection)
                let itemSize = self.layoutAttributesForItem(at: indexPath)?.size ?? CGSize.zero
                if let cellAttribute = self.layoutAttributesForItem(at: indexPath) {
                    if indexPath.item == 0 {
                        leftWidth = inset.left
                        leaveWidth = superWidth - leftWidth - inset.right
                    }
                    if leaveWidth < itemSize.width {
                        leftWidth = inset.left
                        totalHeight += itemSize.height + minLine
                        leaveWidth = superWidth - leftWidth - inset.right
                    }
                    cellAttribute.frame = CGRect.init(x: leftWidth, y: totalHeight, width: itemSize.width, height: itemSize.height)
                    leftWidth += minInter + itemSize.width
                    leaveWidth = superWidth - leftWidth - inset.right
                    attributeArr.append(cellAttribute)
                    if indexPathItem == (numbersOfSection - 1) {
                        totalHeight += itemSize.height
                    }

                }
                
                
                
            }
            //设置组尾布局
            if let footerAttribure = self.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath.init(item: 0, section: indexPathSection)) {
                footerAttribure.frame = CGRect.init(x: 0, y: totalHeight + inset.bottom, width: footerAttribure.frame.width, height: footerAttribure.frame.height)
                totalHeight += footerAttribure.frame.height + inset.bottom
                attributeArr.append(footerAttribure)
            }
            

        }

        
    }
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.attributeArr
    }
    
    
//    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
//        return true
//    }
    override var collectionViewContentSize: CGSize{
        get {
            return CGSize.init(width: self.collectionView?.bounds.width ?? 0, height: self.totalHeight)
        }
    }
    
}

相关文章

网友评论

      本文标题:自定义collectionviewflowLayout

      本文链接:https://www.haomeiwen.com/subject/jiufqktx.html