// // CollectionHFlowLayout.swift // dinoGo // // Created by osell on 2023/1/17. // Copyright © 2023 dino. All rights reserved. // import UIKit class CollectionHFlowLayout: UICollectionViewLayout { var rows: Int = 5 var colums: Int = 2 var itemSize: CGSize = .zero var hSpacing: CGFloat = 10 var vSpacing: CGFloat = 10 var sectionInset: UIEdgeInsets = .zero var attributesArr: [UICollectionViewLayoutAttributes] = [] var contentWidth: CGFloat = 0 var page = 0 // MARK: - Lifecycle private var pageWidth: CGFloat { let width = collectionView?.bounds.width ?? 0 return width > 1 ? width : UIScreen.main.bounds.width } override func prepare() { super.prepare() attributesArr = [] contentWidth = 0 page = 0 let pageTotal = rows * colums guard let itemCount = collectionView?.numberOfItems(inSection: 0) else { return } let pageWidth = self.pageWidth for index in 0.. Bool { abs((collectionView?.bounds.width ?? 0) - newBounds.width) > 0.5 } override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { attributesArr[indexPath.item] } override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { attributesArr.compactMap { if rect.intersects($0.frame) { return $0 } return nil } } override var collectionViewContentSize: CGSize { var height = itemSize.height * CGFloat(rows) height = height + CGFloat(rows - 1) * vSpacing + sectionInset.top + sectionInset.bottom return CGSize(width: CGFloat(page + 1) * pageWidth, height: height) } }