If someone came here looking for MonoTouch's answer, here's what I got by translating Gareth's answer .
The base class that defines EnableAnimationFix and two virtual methods: ApplyAnimationFixToAppearingItem and ApplyAnimationFixToDisappearingItem .
public class CollectionViewFlowLayout : UICollectionViewFlowLayout { protected List<int> _insertedItems = new List<int> (); protected List<int> _deletedItems = new List<int> (); protected virtual bool EnableAnimationFix { get { return false; } } protected virtual void ApplyAnimationFixToAppearingItem (int index, UICollectionViewLayoutAttributes attrs) { throw new NotImplementedException (); } protected virtual void ApplyAnimationFixToDisappearingItem (int index, UICollectionViewLayoutAttributes attrs) { throw new NotImplementedException (); } public override UICollectionViewLayoutAttributes InitialLayoutAttributesForAppearingItem (NSIndexPath path) { var attrs = base.InitialLayoutAttributesForAppearingItem (path); if (!EnableAnimationFix) { return attrs; } attrs = attrs ?? LayoutAttributesForItem (path); if (attrs != null) ApplyAnimationFixToAppearingItem (path.Row, attrs); return attrs; } public override UICollectionViewLayoutAttributes FinalLayoutAttributesForDisappearingItem (NSIndexPath path) { var attrs = base.FinalLayoutAttributesForDisappearingItem (path); if (!EnableAnimationFix) { return attrs; } if (attrs == null && _deletedItems.Contains (path.Row)) {
And here is my actual collection view layout code:
public class DraftsLayout : CollectionViewFlowLayout { // ... protected override bool EnableAnimationFix { get { return true; } } protected override void ApplyAnimationFixToAppearingItem (int index, UICollectionViewLayoutAttributes attrs) { if (_insertedItems.Contains (index)) { SetXByIndex (attrs, index); attrs.ZIndex = -1; } int deletedToTheLeft = _deletedItems.Count (i => i < index); if (deletedToTheLeft > 0) { SetXByIndex (attrs, index + deletedToTheLeft); } } protected override void ApplyAnimationFixToDisappearingItem (int index, UICollectionViewLayoutAttributes attrs) { SetXByIndex (attrs, index); if (_deletedItems.Contains (index)) { attrs.Alpha = 0; } } const int SnapStep = 150; static void SetXByIndex (UICollectionViewLayoutAttributes attrs, int index) { var frame = attrs.Frame; frame.X = index * SnapStep; attrs.Frame = frame; } }
Note that this code should potentially handle multiple deletions in a package nicely.
Kudos to Gareth.
Dan abramov
source share