UICollectionView reloadItemsAtIndexPaths weird animation behavior

I use UICollectionView and reloadItemsAtIndexPaths for maximum efficiency and free animation.

Somehow, animation only starts occasionally (maybe you can use reuse cells?) I created a simple project with one UIViewController , UICollectionView and UIButton to help me start the animation.

Here is my code:

 import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { var cells = [1] @IBOutlet weak var sampleCollectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } @IBAction func buttonPressed(sender: AnyObject) { reloadVisibleSlides() } func reloadVisibleSlides() { self.sampleCollectionView.reloadItemsAtIndexPaths(self.sampleCollectionView.indexPathsForVisibleItems()) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return cells.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell cell.backgroundColor = UIColor(hue: CGFloat(arc4random() % 256) / 256.0, saturation: 1, brightness: 1, alpha: 1) return cell } } 

If you compile it and run it, you will see that these are only triggers.

I also tried to surround it with performBatchUpdates

Is there something I'm doing wrong? Is there any other way to achieve this?

+5
source share

All Articles