美文网首页
支持水平和垂直滚动的UICollectionViewLayout

支持水平和垂直滚动的UICollectionViewLayout

作者: 土豆吞噬者 | 来源:发表于2019-08-14 12:25 被阅读0次
import UIKit

protocol CollectionViewGridLayoutDelegate:class{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: CollectionViewGridLayout,widthForCellInRow row: Int) -> CGFloat
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: CollectionViewGridLayout,heightForCellInSection section: Int) -> CGFloat
}

extension CollectionViewGridLayoutDelegate{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: CollectionViewGridLayout,widthForCellInRow row: Int) -> CGFloat{
        return collectionViewLayout.cellWidth
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: CollectionViewGridLayout,heightForCellInSection section: Int) -> CGFloat{
        return collectionViewLayout.cellHeight
    }
}

class CollectionViewGridLayout:UICollectionViewLayout{
    
    @IBInspectable var cellWidth:CGFloat=50
    @IBInspectable var cellHeight:CGFloat=50
    @IBInspectable var verticalSpacing:CGFloat=1
    @IBInspectable var horizontalSpacing:CGFloat=1
    
    weak var delegate:CollectionViewGridLayoutDelegate?
    
    
    private func getCellWidth(inRow row: Int)->CGFloat{
        if delegate != nil{
            return delegate!.collectionView(collectionView!, layout: self, widthForCellInRow: row)
        }else{
            return cellWidth
        }
    }
    
    private func getCellHeight(inSection section: Int)->CGFloat{
        if delegate != nil{
            return delegate!.collectionView(collectionView!, layout: self, heightForCellInSection: section)
        }else{
            return cellHeight
        }
    }
    
    override var collectionViewContentSize: CGSize {
        let sectionCount = collectionView!.numberOfSections
        var largestCellCount=0
        for i in 0..<sectionCount {
            let cellCount=collectionView!.numberOfItems(inSection: i)
            if cellCount>largestCellCount{
                largestCellCount=cellCount
            }
        }
        var contentWidth:CGFloat=0.0
        var contentHeight:CGFloat=0.0
        for i in 0..<largestCellCount {
            contentWidth += getCellWidth(inRow: i)
            if i>0{
                contentWidth += horizontalSpacing
            }
        }
        for i in 0..<collectionView!.numberOfSections {
            contentHeight += getCellHeight(inSection: i)
            if i>0{
               contentHeight += verticalSpacing
            }
        }
        return CGSize(width: contentWidth, height: contentHeight)
    }
    
    override func layoutAttributesForElements(in rect: CGRect)
        -> [UICollectionViewLayoutAttributes]? {
            var attributesArray = [UICollectionViewLayoutAttributes]()
            let sectionCount = collectionView!.numberOfSections
            for i in 0..<sectionCount {
                let cellCount = collectionView!.numberOfItems(inSection: i)
                for j in 0..<cellCount {
                    let indexPath =  IndexPath(item:j, section:i)
                    let attributes =  layoutAttributesForItem(at: indexPath)
                    attributesArray.append(attributes!)
                }
            }
            return attributesArray
    }
    
    
    override func layoutAttributesForItem(at indexPath: IndexPath)
        -> UICollectionViewLayoutAttributes? {
            let attribute =  UICollectionViewLayoutAttributes(forCellWith:indexPath)
            var cellLeft:CGFloat=0.0
            var cellTop:CGFloat=0.0
            for i in 0..<indexPath.row {
                cellLeft += getCellWidth(inRow: i)
                cellLeft += horizontalSpacing
            }
            for i in 0..<indexPath.section {
                cellTop += getCellHeight(inSection: i)
                cellTop += verticalSpacing
            }
            attribute.frame = CGRect(x:cellLeft, y:cellTop, width:getCellWidth(inRow: indexPath.row),height:getCellHeight(inSection: indexPath.section))
            return attribute
    }
    
    
}

相关文章

网友评论

      本文标题:支持水平和垂直滚动的UICollectionViewLayout

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