我有一个包含 142 个单元格的 UICollectionView。 7.5 随时可见。
我正在将一个单元格从 indexPath 0 移动到 100。
但我也想滚动到那个新位置。
下面的代码工作正常。但它为移动和滚动设置动画,然后加载中央/移动单元格前后的单元格。 我认为这是因为细胞是可重复使用的。但是对于 142,我无法预加载所有这些
这不是最好的效果,我想预加载新位置周围的单元格,indexPath 100 之前4 个和之后4 个,然后看移动和滚动的动画。你能帮忙吗?
UIView.animateWithDuration(animationSpeed, animations: {() -> Void in
self.collectionView.layoutIfNeeded()
self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem:self.indexPathSelected.row,
inSection:0),
atScrollPosition: .CenteredHorizontally,
animated: true)
})
最佳答案
Swift 3.0 OR up
add "view.layoutIfNeeded()" before implementing scrollToItem method, Ideally in viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.layoutIfNeeded()
colView.scrollToItem(at: IndexPath(item: 4, section: 0), at: .centeredHorizontally, animated: true)}
关于swift - UICollectionview scrollToItemAtIndexPath,在动画完成之前不加载可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727635/