SWIFT – iOS – UICOLLECTIONVIEW – When you insert multiple projects, how can you pick one of them, but not at the same time?

If you have a collection view and you change items by inserting several, the default layout will fade them out at the same time. Is it possible to fade them one by one?

I’m thinking about subclassing UICollectionViewLayout, but I’m not sure if this will work like that.

Do I really have to add logic so that it doesn’t happen multiple times at the same time Inserts? (This way I insert a project, wait a minute, insert the second project,…manually complete) There must be a better way. I hope

Thank you for your help

If you want to fade them out at once, you will have to add one to the model and reload that item and then delay (e.g. asyncAfter) and execute the next one Items. Please note that you cannot add all of these to the model at once, just delay reloading the individual items of the collection view. You must postpone the update of the model and reload the corresponding items of the collection view. For example.:

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)])
}
}
}

Yield:

enter image description here

No need to subclass UICollectionViewLayout.

If you have a collection view and you change items by inserting several, the default layout will fade them out at the same time. Is it possible to fade them one by one?

I’m thinking about subclassing UICollectionViewLayout, but I’m not sure if this will work like that.

Do I really have to add logic so that it doesn’t happen multiple times at the same time Inserts? (In this way I insert a project, wait a minute, insert the second project,…manually complete) There must be a better way. I hope

Thank you for your help

If you want to fade them out at once, you will have to add one to the model and reload that item and then delay (e.g. asyncAfter) and execute the next item. Please note that you cannot add all of them at once In the model, you only need to delay reloading the items of the collection view. You must postpone the update of the model and reload the corresponding items of the collection view. For example.:

 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)])< br /> }
}
}

Output:

enter image description here

No need to subclass UICollectionViewLayout.

Leave a Comment

Your email address will not be published.