Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UICollectionView sizeForItemAtIndexPath (Swift)

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
    })

}
like image 763
Tom Coomer Avatar asked Aug 21 '15 15:08

Tom Coomer


1 Answers

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)

}
like image 88
iEngineer Avatar answered Oct 02 '22 01:10

iEngineer