我正在尝试创建需要根据图片居中的滑动组件,因此我设置了layout.sectionInset = UIEdgeInsets (top: 0, left: 32, bottom: 0, right: 32)
当我使用自动滚动调用collection.scrollToItem (at: indexPath, at: .centeredHorizontally, animated: true)
时,它可以正常工作。但是,当我执行手动滑动时,无法集中。我无法更改pointee
使其居中显示在屏幕上。
使用scrollToItem
.centeredHorizontally
let cellWithIncludingSpacing = (collection.layout.itemSize.width + collection.layout.minimumLineSpacing)
var offset = targetContentOffset.pointee
let index = (offset.x + scrollView.contentInset.left) / cellWithIncludingSpacing
let roundedIndex = round(index)
offset = CGPoint(x: roundedIndex * cellWithIncludingSpacing - scrollView.contentInset.left,
y: -scrollView.contentInset.top)
targetContentOffset.pointee = offset
我目前有以下结果: