Animating UICollectionViewCell subviews on layout change

463 views Asked by At

I have two UICollectionViewLayout, one for showing stacked cells and another one to show individual cells on full screen whe they're selected.

I need to animate the cell's subviews to certain positions each time the cell is selected, and then move again those subviews when the cell is deselected.

STEP 1: Cell is selected. Subview is animated from its original position to a new one. No problem here.

STEP 1: Cell is deselected. Subview is animated to another position, BUT the animation starts from the original position, not the new position I indicated in the previous step.

Is there any way to save the modified status, and start the animations from there?

UPDATE

Here's how I change layouts on cell selection (ViewController.m):

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    __weak CustomCellCollectionViewCell *cell = (CustomCellCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    // Hide single view
    if ([indexPath isEqual:self.exposedItemIndexPath]) {
        self.exposedItemIndexPath = nil;
        [cell deselectedAnimations];
        [self animateCollectionView:NO];
    }
    // Select single view
    else if (self.exposedItemIndexPath == nil) {
        self.exposedItemIndexPath = indexPath;
        [cell selectedAnimations];
        [self animateCollectionView:YES];
    }
}

- (void)setExposedItemIndexPath:(NSIndexPath *)exposedItemIndexPath
{
    if (![exposedItemIndexPath isEqual:_exposedItemIndexPath]) {
        if (exposedItemIndexPath) {
            self.audiosContentOffset = self.collectionView.contentOffset;
            SelectedLayout *singleLayout = [[SelectedLayout alloc] initWithExposedItemIndex:exposedItemIndexPath.item];
            [self.collectionView setCollectionViewLayout:singleLayout animated:YES];
        }else{
            self.stackedLayout.overwriteContentOffset = YES;
            self.stackedLayout.contentOffset = self.audiosContentOffset;
            [self.collectionView setCollectionViewLayout:self.stackedLayout animated:YES];
            [self.collectionView setContentOffset:self.audiosContentOffset animated:YES];
        }
        _exposedItemIndexPath = exposedItemIndexPath;
    }
}

selectedAnimations moves a UIView inside the cell, and deselectedAnimations moves it somewhere else when the cell is deselected. BUT, deselectedAnimations starts from the UIView's original position, not from where it was left at the end of selectedAnimations.

The cells and its subviews are based on a nib file. It seems to me that, on layout change, the cell is being rendered again following the "instructions" on the nib file, and that's why the subview is animated starting from its original position instead of where i left it on selectedAnimations.

My question is: is there any way to "save" the cell's subviews positions and start deselectedAnimations from there?

0

There are 0 answers