我正在尝试使用UICollectionViewCompositionalLayout
第三张图片显示更大的地方构建照片网格,所有其他图片都应该很小。
我已经走到了这一步:
问题是这种布局不断重复,所以现在每六张图片中有一张显示得更大,而不是只发生在第一组。
这是我的布局代码:
private func createLayout() -> UICollectionViewLayout {
// Three photos next to eachother
let tripletItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1 / 3), heightDimension: .fractionalHeight(1)))
tripletItem.contentInsets = .init(top: 2, leading: 2, bottom: 2, trailing: 2)
let tripletGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalWidth(1 / 3)),
subitems: [tripletItem]
)
// Two small photos with a bigger photo next to it
let mainItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(2 / 3),
heightDimension: .fractionalHeight(1)
))
mainItem.contentInsets = NSDirectionalEdgeInsets(
top: 2,
leading: 2,
bottom: 2,
trailing: 2
)
let pairItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(0.5)
))
pairItem.contentInsets = NSDirectionalEdgeInsets(
top: 2,
leading: 2,
bottom: 2,
trailing: 2
)
let pairGroup = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1 / 3),
heightDimension: .fractionalHeight(1)
),
subitem: pairItem,
count: 2
)
let mainWithPairGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalWidth(2 / 3)
),
subitems: [pairGroup, mainItem]
)
let nestedGroup = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalWidth(1)
),
subitems: [
mainWithPairGroup,
tripletGroup,
]
)
let section = NSCollectionLayoutSection(group: nestedGroup)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
我如何做到这一点,以便第一组 ( mainWithPairGroup
) 只使用一次,然后tripletGroup
一遍又一遍地重复?