UICollectionViewLayout水平部分,垂直单元格

时间:2017-06-14 13:03:10

标签: ios swift uicollectionview uicollectionviewlayout

我想使用带有自定义布局的UICollectionView,其中集合的各个部分水平滚动,而部分中的项目垂直滚动。

目前我正在使用UICollectionView,然后使用自己的集合视图,但想要更清晰的实现,并想知道是否可以使用自定义UICollectionViewLayout。enter image description here

1 个答案:

答案 0 :(得分:0)

我上传具有自定义集合视图和自定义流程布局的目标C项目,您可以从此处下载,但如果您更喜欢swift,我还会粘贴两个在swift 3.0上翻译的主要类

http://www.filedropper.com/collectionviewinfinitescrollintwodimentions

import UIKit
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
    var itemWidth:CGFloat = 20
    var itemHeight:CGFloat = 20
var space:CGFloat = 10

var columns: Int{
    return self.collectionView!.numberOfItems(inSection: 0)
}
var rows: Int{
    return self.collectionView!.numberOfSections
}


init(itemWidth: CGFloat = 20, itemHeight: CGFloat = 20, space: CGFloat = 5) {
    self.itemWidth = itemWidth
    self.itemHeight = itemHeight
    self.space = space
    super.init()
}

required init(coder aDecoder: NSCoder) {
    self.itemWidth = 20
    self.itemHeight = 20
    self.space = 3
    super.init()
}

override var collectionViewContentSize: CGSize{
    let w : CGFloat = CGFloat(columns) * (itemWidth + space) - 500
    let h : CGFloat = CGFloat(rows) * (itemHeight + space)
    return CGSize(width: w, height: h)
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
    let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
    let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
    attributes.frame = CGRect(x: x, y: y, width: itemWidth + CGFloat(indexPath.row), height: itemHeight)
    return attributes
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
    let maxRow : Int = min(columns - 1, Int(ceil(rect.size.width / (itemWidth + space)) + CGFloat(minRow)))
    var attributes : Array<UICollectionViewLayoutAttributes> = [UICollectionViewLayoutAttributes]()
    for i in 0 ..< rows {
        for j in minRow ... maxRow {
            attributes.append(self.layoutAttributesForItem(at: IndexPath(item: j, section: i))!)
        }
    }
    return attributes
}




import UIKit
class CustomCollectionView: UICollectionView {

    override func layoutSubviews(){
        super.layoutSubviews()
        var currentOffset = self.contentOffset;
        var contentWidth = self.contentSize.width;
        var contentHeight = self.contentSize.height;
        var centerOffsetX = (contentWidth - self.bounds.size.width) / 2.0;
        var centerOffsetY = (contentHeight - self.bounds.size.height) / 2.0;
        var distanceFromCenterX = fabsf(Float(CGFloat(currentOffset.x - centerOffsetX)));
        var distanceFromCenterY = fabsf(Float(CGFloat(currentOffset.y - centerOffsetY)));

        if (CGFloat(distanceFromCenterX) > contentWidth / 4.0) { // this number of 4.0 is arbitrary
            //self.contentOffset = CGPoint(x:centerOffsetX, y:currentOffset.y);
        }
        if (CGFloat(distanceFromCenterY) > contentHeight/4.0) {
            //self.contentOffset = CGPoint(x:currentOffset.x, y:centerOffsetY);
        }
    }
}