CollectionView 简单瀑布流布局

2016-03-17  本文已影响168人  小黑Swift

效果图

Simulator Screen Shot 2016年3月17日 上午12.39.22.png

MyLayout.swift

import UIKit

class MyLayout: UICollectionViewFlowLayout {
    
    var itemCount: Int?
    var attributeArray:Array<UICollectionViewLayoutAttributes>?
    
    override func prepareLayout() {
        super.prepareLayout()
        attributeArray = Array()
        
        let width = (UIScreen.mainScreen().bounds.size.width - self.minimumInteritemSpacing)/2
        var l:CGFloat = 0
        var r:CGFloat = 0
        for i in 0..<itemCount! {
            let indexPath = NSIndexPath(forItem: i, inSection: 0)
            let attri = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
            let height = CGFloat(random()%140 + 50)
            var tmp:CGFloat = 0
            var H:CGFloat = 0
            if l<=r {
                H = l + self.minimumLineSpacing
                l = l + height + self.minimumLineSpacing
                tmp = 0
            } else {
                H = r + self.minimumLineSpacing
                r  = r + height + self.minimumLineSpacing
                tmp = 1
            }
            attri.frame = CGRect(x: tmp * (width + self.minimumInteritemSpacing), y: H, width: width, height: height)
            attributeArray?.append(attri)
        }
        
        if l <= r {
            self.itemSize = CGSizeMake(width, r * 2 / CGFloat(itemCount!) - self.minimumLineSpacing)
        } else {
            self.itemSize = CGSizeMake(width, r * 2 / CGFloat(itemCount!) - self.minimumLineSpacing)
        } 
        
    }
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        return attributeArray
    }

}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //使用
        let myLayout = MyLayout()
        myLayout.itemCount = 30 

        let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: myLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        //注册Cell
        collectionView.registerClass(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "cell")
        self.view.addSubview(collectionView)   
    }
}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor(red: CGFloat(random()%255)/255, green: CGFloat(random()%255)/255, blue: CGFloat(random()%255)/255, alpha: 1)
        //cell.backgroundColor = randonColor()
        return cell
    }
    
    /*随机颜色
    func randonColor() -> UIColor {
        
        let randomR:CGFloat = CGFloat(drand48())
        let randomG:CGFloat = CGFloat(drand48())
        let randomB:CGFloat = CGFloat(drand48())  
        return UIColor(red: randomR, green: randomG, blue: randomB, alpha: 1.0)
    }
    */
}
上一篇下一篇

猜你喜欢

热点阅读