【问题标题】:UICollectionViewLayout horizontal sections, vertical cellsUICollectionViewLayout 水平部分,垂直单元格
【发布时间】:2017-06-14 13:03:09
【问题描述】:

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

目前我使用的是 UICollectionView,然后是具有自己的集合视图的单元格,但我希望实现更简洁的实现,我想知道是否可以使用自定义 UICollectionViewLayout。

【问题讨论】:

  • 我认为自定义布局非常适合您。我可以给你示例代码和项目,并实现自定义布局,看看它是如何工作的。你更喜欢快速还是客观的 C 实现?
  • 太好了,我对 API 不太熟悉,目前迷失在它的文档中 :) - Swift 对我有好处 :) 谢谢!
  • 我上传了目标 C 项目,但如果您有任何问题,欢迎提问:)

标签: ios swift uicollectionview uicollectionviewlayout


【解决方案1】:

我上传了带有自定义集合视图和自定义流程布局的目标 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2016-09-14
    • 2015-07-31
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多