12

I've got a custom UICollectionViewFlowLayout animation that staggers views in from the right with insertions and out to the left with deletions. It does it by setting a CABasicAnimation on the UICollectionViewLayoutAttributes and applying this to the cell layer.

Screen Grab

CollectionViewAnimations Project on GitHub

The default alpha is 0 and its fading out my cells and ending my custom animation early. If I change the alpha to 1 then i don't see my animation at all. I set it at 0.5 and I get a bit of both.... it's weird. You'd have to run my project to see what I mean.

AnimatingFlowLayout.swift

For some reason, I can't seem to completely remove the default alpha on the attributes in finalLayoutAttributesForDisappearingItemAtIndexPath.

Anyone got any ideas?

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112

2 Answers2

1

You're using performBatchUpdates(_:completion:) which already animates the changes you set in finalLayoutAttributesForDisappearingItemAtIndexPath(_:), so if you add the CABasicAnimation you're adding animation to animation that's already gonna happen. If you drop the animation from your CellLayoutAttributes and just set the transform3D of UICollectionViewLayoutAttributes it's gonna do what you want (except the animation beginTime and fillMode). This piece of code works well for me:

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes: CellLayoutAttributes = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath) as! CellLayoutAttributes
    // Default is 0, if I set it to 1.0 you don't see anything happen..'
    attributes.alpha = 1
    let endX = -CGRectGetWidth(self.collectionView!.frame)
    var endTransform: CATransform3D = CATransform3DMakeTranslation(endX, 0, 0)
    attributes.transform3D = endTransform

    return attributes
} 
libec
  • 1,555
  • 1
  • 10
  • 19
  • 1
    To get the animation's `beginTime` effect, you can add your cells one-by-one like they proposed [here](http://aplus.rs/2014/how-to-animate-in-uicollectionview-items/). Also `UICollectionView` uses default duration to animate cell's appearance. And looks like there is no way to change it in a proper/non-hacky way. – sgl0v Aug 13 '15 at 08:57
  • "except the animation beginTime and fillMode" - that's the whole point of this animation... the staggered effect. – bandejapaisa Aug 14 '15 at 08:37
  • 1
    point being you're adding animation to already ongoing animation – libec Aug 14 '15 at 09:04
1

This worked for me, for a similar problem:

import UIKit

class NoFadeFlowLayout: UICollectionViewFlowLayout {

    override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attrs = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
        attrs?.alpha = 1.0
        return attrs
    }

    override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attrs = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
        attrs?.alpha = 1.0
        return attrs
    }

}

You said you couldn't get the default alpha to go away in this method, but it worked when I tried it on tvOS 11.

Matt Mc
  • 8,882
  • 6
  • 53
  • 89