0

我试图在collectionView组合布局的水平滚动部分中获取每个项目的单元格大小。到目前为止,我只尝试获取当前滚动的项目,但没有太多关于单元格大小本身的详细信息。

func dynamicSizingHorizontalScrollSectionLayout() -> NSCollectionLayoutSection {
let item = NSCollectionLayoutItem(
  layoutSize: .init(
    widthDimension: .fractionalWidth(1),
    heightDimension: .estimated(100)
  )
)

let group = NSCollectionLayoutGroup.horizontal(
  layoutSize: .init(
    widthDimension: .absolute(collectionView.frame.width - 40),
    heightDimension: .estimated(100)
  ),
  subitems: [item]
)

group.contentInsets.leading = 10
group.contentInsets.trailing = 10

let section = NSCollectionLayoutSection(group: group)
section.contentInsets.bottom = 10
section.orthogonalScrollingBehavior = .groupPaging

section.contentInsets.leading = 20
section.contentInsets.trailing = 20

section.visibleItemsInvalidationHandler = { [weak self] visibleItems, contentOffset, environment in
  let currentPage = Int(max(0, round(contentOffset.x / environment.container.contentSize.width)))
}

return section

}

4

0 回答 0