swift - collectionView布局

//
//  GA_ViperSortViewController.swift
//  GA_Placer2.0Demo
//
//  Created by houjianan on 2017/2/6.
//  Copyright © 2017年 houjianan. All rights reserved.
//

import UIKit

let kHomeFindLiveFeedCell = "GA_HomeFindLiveFeedCell"
let kHomeSelectedMoreCell = "GA_HomeSelectedMoreCell"

class GA_ViperSortViewController: GA_ViperBaseViewController {
    
    lazy var collectionView: UICollectionView = {
        let c = UICollectionView(frame: CGRect(x: 0, y: kNavigationHeight, width: self.view.ga_w, height: self.view.ga_h - kNavigationHeight), collectionViewLayout: GA_HomeFindLiveFeedCellLayout())
        
        c.register(UINib(nibName: kViperSortHeaderCell, bundle: nil), forCellWithReuseIdentifier: kViperSortHeaderCell)
        c.register(UINib(nibName: kViperSortCell, bundle: nil), forCellWithReuseIdentifier: kViperSortCell)
        
        c.delegate = self
        c.dataSource = self
        c.isPagingEnabled = false
        c.showsVerticalScrollIndicator = false
        c.showsHorizontalScrollIndicator = false
        c.backgroundColor = UIColor.white
        c.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        self.view.addSubview(c)
        
        return c
    }()
    
    override func myViewDidAppear() {
        super.myViewDidAppear()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.ga_show()
    }
}

extension GA_ViperSortViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.row == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kViperSortHeaderCell, for: indexPath) as! GA_ViperSortHeaderCell
            return cell   
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kViperSortCell, for: indexPath) as! GA_ViperSortCell
            cell.sortLabel.text = "12312"
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
        if indexPath.row == 0 {
         
        } else {

        }
    }
}

// MARK: GA_SplitScreenLayout
private class GA_HomeFindLiveFeedCellLayout: UICollectionViewFlowLayout {
    var cellCount: Int = 0
    fileprivate override func prepare() {
        super.prepare()
        cellCount = collectionView!.numberOfItems(inSection: 0)
        print(cellCount)
        let w: CGFloat = AppWidth
        let spcae: CGFloat = 14
        let h: CGFloat = w / 2 - spcae - spcae / 2
        itemSize = CGSize(width: h, height: h)
        sectionInset = UIEdgeInsetsMake(spcae, spcae, spcae / 2, spcae)
        minimumLineSpacing = spcae
        minimumInteritemSpacing = 10
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        if indexPath.row == 0 {
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.size = CGSize(width: collectionView!.ga_w, height: kViperSortHeaderHeight)
            attributes.center = CGPoint(x: collectionView!.ga_w / 2, y: kViperSortHeaderHeight / 2)
            attributes.alpha = 1
            return attributes
        } else {
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            let w: CGFloat = AppWidth
            let space: CGFloat = 14
            let h: CGFloat = w / 2 - space - space / 2
            attributes.size = CGSize(width: h, height: h)
            
            let x = (indexPath.row % 2 == 0) ? (space*2 + h*1.5) : space + h / 2
            let y = CGFloat(indexPath.row - ((indexPath.row % 2 == 1) ? 0 : 1) - (indexPath.row - ((indexPath.row % 2 == 1) ? 0 : 1))/2) * (h + space)
            attributes.center = CGPoint(x: x, y: y)
            attributes.alpha = 1
            return attributes
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attributes = [UICollectionViewLayoutAttributes]()
        for i in 0..<cellCount {
            let indexPath = IndexPath(row: i, section: 0)
            attributes.append(layoutAttributesForItem(at: indexPath)!)
        }
        return attributes
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容