murze
murze

Reputation: 4103

How to put a view on the back of a UICollectionViewCell

In my app there are some UICollectionViewCells displaying some info.

When the user taps a button on one of them, I flip the tapped cells with this piece of code:

UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];

[UIView animateWithDuration:1.0
                      delay:0
                    options:(UIViewAnimationOptionAllowUserInteraction)
                 animations:^
                {
                     NSLog(@"starting animation");

                    [UIView transitionFromView:cell.contentView
                                        toView:cell.contentView
                                      duration:.5
                                       options:UIViewAnimationOptionTransitionFlipFromRight
                                    completion:nil];
                }
                 completion:^(BOOL finished)
                {
                     NSLog(@"animation end");
                }
 ];

After the cell flips (which is does correctly) the cell is completly white.

Two questions about is: - why is the cell white after the flip. Shouldn't it display the original info since the fromView is equal to the toView? - what is the best way to display different content on the back of the cell. I suppose UICollectionViewCell doesn't have something link cell.contentViewBack...

Upvotes: 0

Views: 1874

Answers (1)

craigk
craigk

Reputation: 1304

you may have this by now but,

Not sure if this is the 'Best' way to do it, I got this to work by creating a custom UICollectionViewCell, having 2 UIImageViews in the custom cell, and targeting those views in the animation (this only works for you if thats all you want to have in your cells - may help, may not, anywho)

Create new UICollectionViewCell class (mine is called CardCVCell) In the CardCVCell.h put in you UIImageView outlets

@property (strong, nonatomic) IBOutlet UIImageView *cardImage;
@property (strong, nonatomic) IBOutlet UIImageView *cardBackImage;

I used storyboard - in there I typed in 'CardCVCell' as my custom class on the cell in the Collection View in my scene.

In my View Controller for that scene I have the code you have above, but I use the UIImageViews in the custom cell for the views in the transition (note you have to cast the UICollectionViewCell to your custom class

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
CardCVCell  *cell = (CardCVCell *)[collectionView cellForItemAtIndexPath:indexPath];

         [UIView transitionFromView:cell.cardBackImage
                             toView:cell.cardImage
                           duration:.5
                            options:UIViewAnimationOptionTransitionFlipFromRight
                         completion:^(BOOL finished)
          {
              if (finished) {
                  NSLog(@"animation end");
              }

          }
    ];    
}

I hope this helps someone if not you, if I can help let me know.

cheers

Upvotes: 1

Related Questions