Swift 學(xué)習(xí)之瀑布流布局
import UIKit
//定義協(xié)議
protocol getHeigh {
func itemHeight(_waterFallLayout : Waterlayout, item : Int) -> CGFloat
}
class Waterlayout: UICollectionViewFlowLayout {
var heightDelegat: getHeigh!
/*列數(shù)*/
public var columnCount : Int!
/*縮進(jìn)*/
public var edg : UIEdgeInsets!
/*列間距*/
public var columnMargin : CGFloat!
/*行間距*/
public var rowMargin : CGFloat!
/*高度數(shù)組*/
private lazy var heightArray : NSMutableArray = {
return NSMutableArray()
}()
/*屬性數(shù)組*/
private lazy var attriArray : NSMutableArray = {
return NSMutableArray()
}()
/*準(zhǔn)備布局*/
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)
}
/*返回屬性數(shù)組*/
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return self.attriArray as? [UICollectionViewLayoutAttributes]
}
/*設(shè)置布局屬性*/
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
}
}
調(diào)用布局
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")
}
}