horseshoe7 horseshoe7 - 1 year ago 71
iOS Question

How do I make a 'card stack' kind of UICollectionView?

I'd really like to do something similar to Tinder's "pile of cards" interface, but with a UICollectionView.

I would assume that if you need the other cells to dynamically move given the cell you are interacting with, one would be in Edit Mode on a UICollectionView, but I'm not sure.

Please don't reply with specific Cocoapods that do this and "why don't I just use those?"; it goes beyond just that. I have all sorts of content, and this is just one of the layouts / presentation types I need. Sometimes it's a grid. A lot of the controller actions are the same; just the display and interaction is different.

That said, please DO refer me to any code that may already do this, that I could use as a starting point.

Otherwise, I would be grateful to know where I would get started. I'm thinking maybe one of those projects that re-creates Springboard with a collection view??

EDIT: I was looking for something like a card stack, but not like Tinder in that you can interact with the top card before swiping or letting it return. In the end what I was trying to accomplish was similar to the old iPod-style 'Cover Flow' view.

Answer Source

I'll try to answer my own post. I made a few incorrect assumptions. In the end it was a lot less complex than I thought.

To get a collection view whose layout isn't just 'static within a scrollview' but changes dynamically with the contentOffset property, you need to subclass UICollectionViewLayout and make sure first of all you return YES for - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

from there, most of your heavy lifting will be done in the - (void)prepareLayout method.

I basically used the following tutorial as a starting point and kind of followed what they were trying to do (or just downloaded the final project and saw how they did it, and mimicked the same effect), but obviously had a different prepareLayout method:

Ray Wenderlich

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download