I'm using a UICollectionView
and reloadItemsAtIndexPaths
to achieve maximum efficiency and get the animations for free.
Somehow the animation is triggering only sometimes (maybe cause reuse cells? ) I've created a simple project with one UIViewController
, a UICollectionView
and a UIButton
to help me trigger 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 and run it you will see it only triggers alternatively.
I've also tried surrounding it with performBatchUpdates
Is there something I am doing wrong? Is there another way achieve this?