UICollectionview scrollToItemAtIndexPath,在动画完成之前不加载可见单元格

Thu*_*ris 3 animation scroll uicollectionview uicollectionreusableview swift

我有UICollectionView142个细胞.任何时候都可以看到7.5.

我正在将一个单元格从indexPath0移到100.但我也想滚动到那个新位置.

下面的代码工作正常.但它动画移动和滚动,但随后将细胞加载到中央/移动的细胞前后.我认为这是因为细胞是可重复使用的.但是142,我无法预加载所有这些

它不是最好的效果,我想预先加载新位置周围的单元格,4之前和之后4 indexPath,然后看动画的动画和滚动.你能帮帮忙吗?

UIView.animateWithDuration(animationSpeed, animations: {() -> Void in
    self.collectionView.layoutIfNeeded()
    self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem:self.indexPathSelected.row, 
                                                 inSection:0), 
                                                 atScrollPosition: .CenteredHorizontally, 
                                                 animated: true)

})
Run Code Online (Sandbox Code Playgroud)

Hir*_*hal 18

Swift 3.0或更高版本

在实现scrollToItem方法之前添加"view.layoutIfNeeded()",理想情况下在viewWillAppear中

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.layoutIfNeeded()
  colView.scrollToItem(at: IndexPath(item: 4, section: 0), at: .centeredHorizontally, animated: true)}
Run Code Online (Sandbox Code Playgroud)


Wil*_*jay 7

我使用dispatch_async来更新UI并且它可以工作.

let numberOfSections = self.collectionView.numberOfSections()
let numberOfRows = self.collectionView.numberOfItemsInSection(numberOfSections-1)

    if numberOfRows > 0 {
        dispatch_async(dispatch_get_main_queue(), {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
        })
    }
Run Code Online (Sandbox Code Playgroud)