Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UICollectionView - When inserting multiple items at once, how can you fade them in one after the other instead of simultaneously?

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

like image 923
Quantm Avatar asked Dec 04 '17 17:12

Quantm


1 Answers

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:

enter image description here

No subclassing of UICollectionViewLayout is needed.

like image 171
Rob Avatar answered Nov 15 '22 08:11

Rob