UICollectionViewLayout doesn't update zIndex when changing layouts
I have two UICollectionViewLayouts that work fine when used on their own
but when switching between the two layouts using
setCollectionViewLayout:animated: the zIndex of the layout being animated
to doesn't use its own zIndex values, it instead uses the zIndex values of
the original layout.
Here are what the two layouts look like on their own:
In layout 1 the cells are stacked with item 0 on top.
In layout 2 the cells are stacked with item 0 on the bottom.
In layout 1 I set the zIndex with:
[attributes setZIndex:100 - [indexPath item]];
In layout 2 I set the zIndex with:
[attributes setZIndex:100 + [indexPath item]];
If I start with layout 1 and change to layout 2 I end up with this:
If I start with layout 2 and change to layout 1 I end up with this:
I've experimented with setting the zIndex in both
layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath:
and what I've found is that when switching layouts,
layoutAttributesForElelmentsInRect: gets called once on the layout that is
being animated to and layoutAttributesForItemAtIndexPath: gets called many
times on both layouts as the animated transition is happening.
I also noticed that the calls to layoutAttributesForItemAtIndexPath: were
being called on the layout being animated from after being called on the
layout being animated to. This lead me to try something odd, which was to
set the correct zIndex for a given layout in
layoutAttributesForElementsInRect: but to set the zIndex for the other
layout in layoutAttributesForItemAtIndexPath:. This actually results in
the correct layout after the animation is completed in either direction
but the animation from layout 2 to layout 1 looks bad because the zIndex
doesn't update till the very end of the animation.
Here is what it looks like right before the animation from layout 2 to
layout 1 completes (and corrects the zIndex):
To deal with seeing this in between state I am considering making an
intermediate layout that only changes the zIndex of the cells in layout 2
before it transitions to layout 1. Not elegant but I think it may work if
I use setCollectionViewLayout:animated:completion: where I call
setCollectionViewLayout:animated: inside the completion block of the first
layout change.
Anyway, it seems crazy to me that I should have to set the zIndex for the
opposite layout in layoutAttributesForItemAtIndexPath: in order to get the
correct end result of changing layouts. Does anyone have any suggestions
for how to implement this in a way that doesn't require this hack?
No comments:
Post a Comment