Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCell flip and grow on tap

How can I achieve the animation where a UICollectionViewCell with flip and grow to show modal view on tap?

like image 422
nicktones Avatar asked Dec 07 '12 14:12

nicktones


2 Answers

Here is what I used in another project and it was working well :

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    if (cell.selected) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        [UIView transitionWithView:cell
                          duration:0.2
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{
                            [cell setFrame:self.selectedCellDefaultFrame];
                            cell.transform = self.selectedCellDefaultTransform;
                        }
                        completion:^(BOOL finished) {
                            self.selectedCellDefaultFrame = CGRectZero;
                            [collectionView reloadItemsAtIndexPaths:@[indexPath]];
                        }];
        return NO;
    }
    else {
        return YES;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    [cell.superview bringSubviewToFront:cell];
    self.selectedCellDefaultFrame = cell.frame;
    self.selectedCellDefaultTransform = cell.transform;
    [UIView transitionWithView:cell
                      duration:0.2
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [cell setFrame:collectionView.bounds];
                        cell.transform = CGAffineTransformMakeRotation(0.0);
                    }
                    completion:^(BOOL finished) {}];
}

Different things here :

  • The bringSubviewToFront: message call is used to prevent the cell to animate behind the other cells
  • We use two properties declared in the controller : selectedCellDefaultFrameand selectedCellDefaultTransform to save the default state of the cell and reinitialize it when deselecting
  • When deselecting, we call the reloadItemsAtIndexPaths: method of UICollectionView to be sure that the reset of the position is totally complete

Let me know if you have any trouble with this.

Good luck,

like image 181
Zedenem Avatar answered Oct 22 '22 17:10

Zedenem


I haven't tried the grow animation, but I think I can help with the UICollectionViewCell flip animation.

Try:

UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];

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

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

Hope that helps!

like image 38
Janum Trivedi Avatar answered Oct 22 '22 16:10

Janum Trivedi