kidsid49
kidsid49

Reputation: 1378

How to maintain the states of other UICollectionView cells after calling deleteItemsAtIndexPaths?

As "deleteItemsAtIndexPaths" documentation says

"The collection view updates the layout of the remaining items to account for the deletions, animating the remaining items into position as needed."

In my cases while deleting a cell from collection view its all cells are doing some animation but once i call deleteItemsAtIndexPaths the old animating cells who moved to their new position stop doing that animation?

So my issue is that how the keep that state same after moving cells to new position because of deleteItemsAtIndexPaths?

Edit:- My all cells are scaled down and doing wobbling pretty similar to "when user long press on app icon to delete app in iPhone.

[UIView animateWithDuration:0.125
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
    self.transform = CGAffineTransformMakeScale(0.8,0.8);
} completion:^(BOOL finished){

    if(finished) {
        [self shouldAllowInteraction:NO];
        CGAffineTransform leftWobble = CGAffineTransformRotate(self.transform, RADIANS(-1.0));
        CGAffineTransform rightWobble = CGAffineTransformRotate(self.transform, RADIANS(1.0));
        self.transform = leftWobble;
        [UIView animateWithDuration:0.125
                              delay:0 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction)
                         animations:^{
            self.transform = rightWobble;

        } completion:^(BOOL finished) {

        }];
    }
}];

But when i tried to delete a cell using

 [cardsViewController.collectionView deleteItemsAtIndexPaths:indexPathArray];

All moved cells - (Cells after the deleted cell) stops doing that wobbling and scaled up. They don't retain same behaviour means animation.

eg. Sample gif is here. enter image description here

Upvotes: 3

Views: 1244

Answers (2)

Clay Garrett
Clay Garrett

Reputation: 1023

Could this answer help you out?

Avoid animation of UICollectionView after reloadItemsAtIndexPaths

Looks like a way to disable certain animations for the UICollection view (modified the code in the answer to suit your issue):

[UIView animateWithDuration:0 animations:^{
    [collectionView performBatchUpdates:^{
        [collectionView deleteItemsAtIndexPaths:indexPaths];
    } completion:nil]; 
}];

Upvotes: 1

Deekshith Bellare
Deekshith Bellare

Reputation: 735

If i understood your requirement correct
a) Scale down and wobble
enter image description here
b) on delete and rearrange, do not scale up/down but continue to wobble
enter image description here

add these methods to your cell

-(void)scaleDownAndWoble
{
    [UIView animateWithDuration:1
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.transform = CGAffineTransformMakeScale(0.8,0.8);
                     } completion:^(BOOL finished){

                         if(finished) {
                             [self wobble];
                         }
                     }];


}

-(void)wobble
{
    CGAffineTransform leftWobble = CGAffineTransformRotate(self.transform, DEGREE(-1.0));
    CGAffineTransform rightWobble = CGAffineTransformRotate(self.transform, DEGREE(1.0));
    self.transform = leftWobble;
    [UIView animateWithDuration:0.125
                          delay:0 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction)
                     animations:^{
                         self.transform = rightWobble;

                     } completion:^(BOOL finished) {

                     }];


}

Now for step 1: Call scaleDownAndWoble for each visible cell Step2 : call wobble for all visible cell after delete animation completes

 - (void)deleteItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSMutableArray *colorNames = self.sectionedColorNames[indexPath.section];
        [colorNames removeObjectAtIndex:indexPath.item];
        [self.collectionView performBatchUpdates:^{
            [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];

        }
                                      completion:^(BOOL finished) {
                                          NSArray *array = [self.collectionView visibleCells];
                                          for( ColorNameCell * cell in array)
                                          [cell wobble];

                                      }];
    }

Upvotes: 4

Related Questions