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..<columnCount {
            self.heightArray.add(self.edg.top)
        }
        self.attriArray.removeAllObjects()
        
        let itemsCount = self.collectionView?.numberOfItems(inSection: 0)
        
        for i in 0..<itemsCount! {
            let attri = self.layoutAttributesForItem(at: NSIndexPath.init(row: i, section: 0) as IndexPath)
            self.attriArray.add(attri!)
        }
        
    }
    /*collectionView的大小*/
    
    override var collectionViewContentSize: CGSize{
        var maxColH:CGFloat = self.heightArray.firstObject as! CGFloat
        self.heightArray.enumerateObjects { (obj, index, stop) in
            let  obj = obj as! CGFloat
            if maxColH < obj {
                maxColH = obj
            }
        }
        
        return CGSize(width: (self.collectionView?.frame.size.width)!, height: maxColH + self.edg.bottom)
    }
    
    /*返回属性数组*/
    override func layoutAttributesForElements(in rect: CGRect) -> [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学习资料@完整App@App框架@ 响应式框架@ UI@ 日历三方库@下拉刷新@模糊效果@富文本@图...
    IOS开发攻城狮_Fyc阅读 6,344评论 1 90
  • 目录 Swift学习资料@ 完整App@ App框架@响应式框架@ UI@日历三方库@ 下拉刷新@ 模糊效果@ 富...
    Dear_80d3阅读 8,640评论 2 79
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS[https://github...
    香橙柚子阅读 8,757评论 0 36
  • 今天是dinggie同学的生日 在这个普天同庆的日子首先跟你说 然后 我再跟你说说心里话 高中刚报道的时候 我妈看...
    不知道起啥名啊啊阅读 249评论 0 0
  • 成都七大必去景点(两天半时间) 1 宽窄巷子 2 锦里 3大熊猫繁育研究基地 (49路转87路 1小时12分钟 4...
    娜娜是最美的小仙女阅读 218评论 0 2