Preventing “wrapping” of items in UICollectionView

前端 未结 3 1073
执笔经年
执笔经年 2021-02-09 06:09

I need a UICollectionView to display a grid that is potentially larger than the visible frame in both width and height, while maintaining row and column integrity.

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-09 06:10

    Here is a version of AndrewK's code updated to Swift 4:

    import UIKit
    
    class CollectionViewMatrixLayout: UICollectionViewLayout {
    
      var itemSize: CGSize
      var interItemSpacingY: CGFloat
      var interItemSpacingX: CGFloat
      var layoutInfo: [IndexPath: UICollectionViewLayoutAttributes]
    
      override init() {
        itemSize = CGSize(width: 50, height: 50)
        interItemSpacingY = 1
        interItemSpacingX = 1
        layoutInfo = [IndexPath: UICollectionViewLayoutAttributes]()
    
        super.init()
      }
    
      required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
    
      override func prepare() {
        guard let collectionView = self.collectionView else {
          return
        }
    
        var cellLayoutInfo = [IndexPath: UICollectionViewLayoutAttributes]()
        var indexPath = IndexPath(item: 0, section: 0)
    
        let sectionCount = collectionView.numberOfSections
        for section in 0.. CGRect {
        let row = indexPath.section
        let column = indexPath.item
    
        let originX = (itemSize.width + interItemSpacingX) * CGFloat(column)
        let originY = (itemSize.height + interItemSpacingY) * CGFloat(row)
    
        return CGRect(x: originX, y: originY, width: itemSize.width, height: itemSize.height)
      }
    
      override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
      {
        var allAttributes = Array()
    
        for (_, attributes) in self.layoutInfo {
          if (rect.intersects(attributes.frame)) {
            allAttributes.append(attributes)
          }
        }
        return allAttributes
      }
    
      override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return self.layoutInfo[indexPath]
      }
    
      override var collectionViewContentSize: CGSize {
        guard let collectionView = self.collectionView else {
          return .zero
        }
    
        let sectionCount = collectionView.numberOfSections
        let height = (itemSize.height + interItemSpacingY) * CGFloat(sectionCount)
    
        let itemCount = Array(0..

提交回复
热议问题