Avoid animation of UICollectionView after reloadItemsAtIndexPaths

后端 未结 9 1846
情深已故
情深已故 2020-12-04 06:07

UICollectionView animate items after reloadItemsAtIndexPaths is called (fade animation).

Is there a way to avoid this animation?

iOS 6

9条回答
  •  执念已碎
    2020-12-04 06:24

    Here is a Swift 3 version to performBatchUpdates without animation to a UICollectionView. I found this to work better for me than collectionView.reloadData() because it reduced cell swapping when records were inserted.

    func appendCollectionView(numberOfItems count: Int){
    
            // calculate indexes for the items to be added
            let firstIndex = dataItems.count - count
            let lastIndex = dataItems.count - 1
    
            var indexPaths = [IndexPath]()
            for index in firstIndex...lastIndex {
                let indexPath = IndexPath(item: index, section: 0)
                indexPaths.append(indexPath)
            }
    
       UIView.performWithoutAnimation {
    
            self.collectionView.performBatchUpdates({ () -> Void in
                self.collectionView.insertItems(at: indexPaths)
            }, completion: { (finished) -> Void in
    
            })
        }
    }
    

提交回复
热议问题