Animate UICollectionView sizeForItemAtIndexPath (Swift)
Asked Answered
T

1

6

I would like to animate the resizing of a UICollectionViewCell. I have written the code below but cannot have the return line inside the animation block. Any ideas?

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var newSize = CGSize(width: (self.view.frame.width), height: 0)


    UIView.animateWithDuration(2.0, animations: { () -> Void in
        return newSize
    })

}
Typeset answered 21/8, 2015 at 15:28 Comment(0)
U
2

Call following method if you want to animate,

self.collectionView.performBatchUpdates(updates: (() -> Void), completion:((Bool) -> Void)?)

More specifically, you should also handle orientation change, like below,

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)
{
    self.collectionView.performBatchUpdates(nil, completion: nil)

}
Unchancy answered 31/5, 2016 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.