我用 groupPagingCentered 的正交部分做了 UICollectionViewCompositionalLayout 如下:
func neighborsRealitiesSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let cellWidth = UIScreen.main.bounds.width / 3 + 1
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(cellWidth), heightDimension: .absolute(cellWidth))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPagingCentered
section.visibleItemsInvalidationHandler = { items, offset, environment in
items.filter { $0.representedElementKind != UICollectionView.elementKindSectionHeader }.forEach { item in
let distanceFromCenter = abs((item.frame.midX - offset.x) - environment.container.contentSize.width / 2.0)
let minScale: CGFloat = 0.7
let maxScale: CGFloat = 1
let scale = max(maxScale - (distanceFromCenter / environment.container.contentSize.width), minScale)
item.transform = CGAffineTransform(scale: scale)
}
}
}
其他部分垂直滚动。垂直滚动时,正交偏移重置为零。如果我评论 visibleItemsInvalidationHandler,它不会被重置。
为什么会这样?有人有这个问题吗?