Mark
Mark

Reputation: 6977

How to animate a relayout of NSCollectionViewLayout on bounds change?

I have a basic implementation of NSCollectionViewLayout very similar to NSCollectionViewFlowLayout:

It places item in a horizontal row. If there's not enough room, the next item is added to the next row and so on.

My question: how can I animate a layout change when an item moves from the first row into the second row?

Here's an example:

Relayout

Note how item #3 "jumps" from the first to the second row. I'd like to animate that change.

There are lots of methods to animate adding/removing items, but I was not able to figure out how to animate layout changes when no items change.

Upvotes: 2

Views: 621

Answers (1)

sbrun
sbrun

Reputation: 304

The methods you're looking to override in your layout subclass are prepare(forAnimatedBoundsChange:) and finalizeAnimatedBoundsChange().

From the documentation:

open func prepare(forAnimatedBoundsChange oldBounds: NSRect) // NSCollectionView calls this when its bounds have changed inside an animation block before displaying items in its new bounds

Prepares the layout object for animated changes to the collection view’s bounds or for the insertion or deletion of items.

open func finalizeAnimatedBoundsChange() // also called inside the animation block

Cleans up after any animated changes to the collection view’s bounds or after the insertion or deletion of items.

Upvotes: 0

Related Questions