What is the best best way to disable cross fades on UICollectionView/UICollectionViewLayout rotates or bounds changes?
-
11-12-2019 - |
Вопрос
I have a subclass of UICollectionViewLayout
which places cells in a circle. The layout returns YES
for the call shouldInvalidateLayoutForBoundsChange:
. On rotation, the cell in the initial position fades out and the cell in the final position fades in.
By adding the following code to my layout I can disable the fades and the circle of items appears to to simply rotate with the orientation change:
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
return nil;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
}
Why do the methods get called on a bounds change because documentation doesn't seem to suggest they do? Documentation seems to state they get called related to insertion and removal of items from the collection view.
Is there a better way to disable the cross fade during rotation?
Notes:
- The
initialLayoutAttributesForAppearingItemAtIndexPath:
documentation states that by default the method returnsnil
but calls tosuper
returned non-nil values. - I set symbolic breakspoints on the
UICollectionView
methodsdeleteItemsAtIndexPaths:
,moveItemAtIndexPath:toIndexPath:
andinsertItemsAtIndexPaths:
and none of them are hit during rotation.
Решение
The UICollectionViewLayout.h
file states
// This set of methods is called when the collection view undergoes an
animated transition such as a batch update block or an animated
bounds change.
// For each element on screen before the invalidation,
finalLayoutAttributesForDisappearingXXX will be called and an
animation setup from what is on screen to those final attributes.
// For each element on screen after the invalidation,
initialLayoutAttributesForAppearingXXX will be called an an
animation setup from those initial attributes to what ends up on
screen.
which clearly says they are called on bounds changes. Rather than removal/insertion, "old state" and "new state" seems more accurate.