Swift 学习之瀑布流布局

Swift 学习之瀑布流布局

import UIKit
//定义协议
protocol getHeigh {
    func itemHeight(_waterFallLayout : Waterlayout, item : Int) -> CGFloat
}
class Waterlayout: UICollectionViewFlowLayout {
    
     var heightDelegat: getHeigh!
    /*列数*/
    public var columnCount : Int!
    /*缩进*/
    public var edg : UIEdgeInsets!
    /*列间距*/
    public var columnMargin : CGFloat!
    /*行间距*/
    public var rowMargin : CGFloat!
    /*高度数组*/
    private lazy var heightArray : NSMutableArray = {
        return NSMutableArray()
    }()
    /*属性数组*/
    private lazy var attriArray : NSMutableArray = {
       return NSMutableArray()
        }()
    
    /*准备布局*/
    override func prepare() {
        super.prepare()
        self.heightArray.removeAllObjects()
        
        for _ in 0.. [UICollectionViewLayoutAttributes]? {
        return self.attriArray as? [UICollectionViewLayoutAttributes]
    }
    /*设置布局属性*/
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attribute:UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
    /*cell的宽度*/
        let w = ((self.collectionView?.frame.size.width)! - self.edg.left - self.edg.right - self.columnMargin * (CGFloat(self.columnCount)-1)) / CGFloat(self.columnCount)
    /*cell的高度*/
        let  h = self.heightDelegat.itemHeight(_waterFallLayout: self, item: indexPath.item)
        
    /*cell--x*/
        var indexCol = 0
        var minCoH = self.heightArray[0] as! CGFloat
        
        self.heightArray.enumerateObjects { (obj , index, stop) in
            let colH = obj as! CGFloat
            if (colH < minCoH){
                minCoH = colH
                indexCol = index
            }
        }
        
        let x = self.edg.left + (self.columnMargin + w) * CGFloat(indexCol)
        var y = minCoH  + self.rowMargin
        if minCoH == self.edg.top {
            y = self.edg.top
        }
        attribute.frame = CGRect(x: x, y: y, width: w, height: h)

        self.heightArray[indexCol] = attribute.frame.maxY
        
      return attribute
    }
    

}

调用布局

import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,getHeigh {
    func itemHeight(_waterFallLayout: Waterlayout, item: Int) -> CGFloat {
        return self.heiArray[item] as! CGFloat
    }
    
    
    lazy var collection : UICollectionView = {  
        let layout = Waterlayout()
        layout.columnCount = 2
        layout.columnMargin = 10
        layout.heightDelegat = self
        layout.rowMargin = 10
        layout.edg = UIEdgeInsetsMake(10, 10, 10, 10)
        let temcollection = UICollectionView(frame:self.view.bounds, collectionViewLayout: layout)
        temcollection.delegate = self
        temcollection.dataSource = self
        temcollection.backgroundColor = UIColor.white
        
        return temcollection
    }()
    
    lazy var heiArray:NSArray = {
        return [100.0,80.0,60.0,87.0,79.0,80.0,90.0,110.0,134.0,162.0,147.0,185.0,162.0,155.0,142.0,148.0,139.0,162.0]
    }()
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        
        return self.heiArray.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        for  vi:UIView in cell.contentView.subviews {
            vi.removeFromSuperview()
        }
        let  lable = UILabel(frame: cell.bounds)
        lable.text = "\(indexPath.item)"
        lable.textColor = UIColor.red
        lable.textAlignment = NSTextAlignment.center
        cell.backgroundColor = UIColor.purple
        cell.contentView.addSubview(lable)
        return cell
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.addSubview(self.collection)
        
        self.collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
    }

    


}


你可能感兴趣的:(Swift 学习之瀑布流布局)