UICollectionViewCell等边距的处理方式

//
//  MMSearchStyleFlowLayout.swift
//  storefront-ios
//
//  Created by Demon on 18/7/18.
//  Copyright © 2018年 WWE & CO. All rights reserved.
//

import UIKit

enum MMAlignType : NSInteger {
    case left = 0
    case center = 1
    case right = 2
}

class MMSearchStyleFlowLayout: UICollectionViewFlowLayout {
    //两个Cell之间的距离
    private var betweenOfCell : CGFloat{
        didSet{
            self.minimumInteritemSpacing = betweenOfCell
        }
    }
    //cell对齐方式
    private var cellType : MMAlignType = MMAlignType.center
    //在居中对齐的时候需要知道这行所有cell的宽度总和
    private var sumCellWidth : CGFloat = 0.0
    
    override init() {
        betweenOfCell = 5.0
        super.init()
        scrollDirection = UICollectionViewScrollDirection.vertical
        minimumLineSpacing = 5
        sectionInset = UIEdgeInsetsMake(5, 15, 5, 15)
    }
    
    convenience init(_ cellType:MMAlignType){
        self.init()
        self.cellType = cellType
    }
    
    convenience init(_ cellType: MMAlignType, _ betweenOfCell: CGFloat){
        self.init()
        self.cellType = cellType
        self.betweenOfCell = betweenOfCell
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let layoutAttributes_super : [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: rect) ?? [UICollectionViewLayoutAttributes]()
        let layoutAttributes:[UICollectionViewLayoutAttributes] = NSArray(array: layoutAttributes_super, copyItems:true)as! [UICollectionViewLayoutAttributes]
        var layoutAttributes_t : [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
        
        for index in 0..

你可能感兴趣的:(UICollectionViewCell等边距的处理方式)