3

I'm using a UICollectionView, but am using it somewhat like a TableView. I would like to animate cells away by having them animate away to the left and new cells animate in from the right. With the UITableView, I could just UITableViewRowAnimation.Right animation while inserting / removing cells. How might one go about performing something like this with a UICollectionView?

joslinm
  • 7,212
  • 6
  • 46
  • 66
  • Like Springboard? How many cells are we talking about here? Do you just want the animation or do you want the rest of the tableView functionality? – CaptJak Feb 03 '15 at 15:31
  • I want the animation, so I would like to add cells in from the right while removing cells to the left. – joslinm Feb 03 '15 at 15:45
  • maybe: http://stackoverflow.com/questions/19301762/uicollectionview-horizontal-scroll-horizontal-layout – CaptJak Feb 03 '15 at 15:46

1 Answers1

2

Unfortunately there is no native implementation of such animations in UICollectionView. You can implement them by creating a UICollectionViewLayout (or UICollectionViewFlowLayout) subclass and overriding these two methods:

// This set of methods is called when the collection view undergoes an animated transition such as a batch update block or an animated bounds change.
// For each element on screen before the invalidation, finalLayoutAttributesForDisappearingXXX will be called and an animation setup from what is on screen to those final attributes.
// For each element on screen after the invalidation, initialLayoutAttributesForAppearingXXX will be called an an animation setup from those initial attributes to what ends up on screen.

func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

i.e. initialLayoutAttributesForAppearingItemAtIndexPath has to return UICollectionViewLayoutAttributes for the element that is being added, with position (attributes.center or .frame.origin) from which it will be animated into its position within the table.

Note that UICollectionViewLayoutAttributes also has alpha, transform and several more parameters you can use to tweak the animation.

Nikita Kukushkin
  • 13,408
  • 4
  • 33
  • 45