when you have a collection view and you're changing the items by inserting a few, the default layout fades them in simultaneously. Is there a possibility to fade them in one after the other?
I was thinking about subclassing UICollectionViewLayout
, but I'm not sure if that's going to work like that.
Do I really have to add logic so that multiple inserts at once don't occur? (so that I insert an item, wait a second, insert the second item, ... Doing it manually) There has to be a better way. I hope
Thanks for any help
If you want to fade them in one at a time, you are going to have to add one to your model and reload that one item and then delay (e.g. asyncAfter
) and do the next one. Note, you cannot add all of them to your model at once and simply defer the reloading of the individual items of the collection view. You have to defer both the update of the model and the reloading of the appropriate item of the collection view. E.g.:
private func append(_ objectsToAdd: [Foo]) {
for i in 0 ..< objectsToAdd.count {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(i) * 0.25) {
self.objects.append(objectsToAdd[i])
self.collectionView?.insertItems(at: [IndexPath(item: self.objects.count - 1, section: 0)])
}
}
}
That yields:
No subclassing of UICollectionViewLayout
is needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With