0

I have a UICollectionView with in the first, view-wide, cell an image. Now the image has to disappear from sight after entering, animated.

My first thought was to just delete the cell. The animation for that looks exactly like what I want. Except a huge amount of code is dependant on the amount of cells being static. Perhaps things should have been designed differently from the start. Alas, it's too late for that now.

My second thought was to make the height of the cell 0. I wrote an animation for that, based on this answer, but I can't seem to get it to work. The animation seems instant and I have no idea how to get the rest of the UICollectionView to animate with it.

Here is my existing code:

// version 1
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.needToClearImage) {
        self.needToClearImage = NO;
        void (^compressCell)() = ^() {
            [self.collectionViewGrid reloadData];
        };

        [UIView transitionWithView:cellToCompress duration:2.0f options:UIViewAnimationOptionCurveLinear animations:compressCell completion:nil];
     }
}

// version 2
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.needToClearImage) {
        self.needToClearImage = NO;
        UICollectionViewCell *cellToCompress = [self.collectionViewGrid cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        void (^compressCell)() = ^() {
            CGRect frame = cellToCompress.frame;
            frame.size = CGSizeMake(0, 0);
            cellToCompress.frame = frame;
        };

        [UIView transitionWithView:cellToCompress duration:2.0f options:UIViewAnimationOptionCurveLinear animations:compressCell completion:nil];
     }
}

// used in both versions
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0 && self.needToClearImage == NO) {
        return CGSizeMake(self.collectionViewGrid.frame.size.width, 0);
    }
    else {
        ...
    }
}

Edit:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.needToClearImage) {
        self.needToClearImage = NO;
        [self.collectionViewGrid performBatchUpdates:^{
        } completion:^(BOOL finished) {
        }];
    }
}

Calling performBatchUpdates seems to animate the updating of the UICollectionView. It does not however animate the disappearing of the image.

Community
  • 1
  • 1
Belle
  • 273
  • 2
  • 15

0 Answers0