How do I maintain the states of other cells in the UICollectionView after calling deleteItemsAtIndexPaths?

As the documentation says "deleteItemsAtIndexPaths"

"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, when deleting a cell from the collection view, all the cells do some animation, but as soon as I call deleteItemsAtIndexPaths, the old animation cells that have moved to the new position stop doing this animation?

So my problem is how to keep this state after moving cells to a new position due to deleteItemsAtIndexPaths?

Edit: - All my cells are zoomed out and tend to work like "when the user long clicks on the app icon to uninstall the app on the 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 the cell using

 [cardsViewController.collectionView deleteItemsAtIndexPaths:indexPathArray];

      

All moved cells - (cells after the deleted cell) stop doing this wobble and are scaled. They do not retain the same behavior as animation.

eg. An example of a gif is here. enter image description here

+3


source to share


2 answers


If I understood your requirement correctly a) Scaling and wobble
enter image description here
b) when deleting and rearranging, do not zoom in / out, but keep wobbling
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 Step 2: Call wobble for the entire visible cell after the completion animation has finished.

 - (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];

                                      }];
    }

      

+4


source


Can this answer help you?

Avoid animating UICollectionView after reloadItemsAtIndexPaths



Similar to the way to disable certain animations for the UICollection view (changed the code in the answer to suit your problem):

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

      

+1


source







All Articles