Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UICollectionViewFlowLayout animation

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?

like image 508
bandejapaisa Avatar asked Aug 03 '15 21:08

bandejapaisa


2 Answers

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
} 
like image 69
libec Avatar answered Oct 18 '22 16:10

libec


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.

like image 29
Matt Mc Avatar answered Oct 18 '22 16:10

Matt Mc