3

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

kidsid49
  • 1,358
  • 3
  • 18
  • 37
  • Hmm. I *think* I understand the issue, but can you post some example code demonstrating the problem? – Ash Furrow Oct 12 '14 at 07:46
  • @ash Updated My situation is quite similar to "When user tries to delete an app from iPhone" so when user deletes an app still the shifted app icons doesn't stop animation. – kidsid49 Oct 12 '14 at 10:52
  • For this kind of thing, you're probably going to have to go beyond simple UIView-based animations and use Core Animation `CAAnimation` classes on the layers themselves. Collection views just weren't meant to work in this way. – Ash Furrow Oct 13 '14 at 08:50

2 Answers2

4

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

                                      }];
    }
Deekshith Bellare
  • 725
  • 2
  • 6
  • 19
  • I am doing that kind of stuff already the thing is deleteItemsAtIndexPaths does the scale up animation on moved cell so i am able to start wobbling gaian on moved cells again but for a scales up and then wobble animation starts. Its like a glitch for a moment – kidsid49 Oct 15 '14 at 12:15
  • @kidsid49 Check the gif attached,its working for me, don't reload the collection view rather apply wobble animation to visible cells – Deekshith Bellare Oct 15 '14 at 12:24
  • what are you passing in sizeForItemAtIndexPath is it scaled down size? And i guess the scaled down animation is in your case is custom flowlayout thing? – kidsid49 Oct 15 '14 at 12:47
  • @kidsid49 Yes, Its a custom layout with size(80,80). But it works well without using custom layout as well. IF you are still facing the problem give a link to the sample code. I'l check – Deekshith Bellare Oct 15 '14 at 13:16
  • so size (80,80) is scaled down one or original size? – kidsid49 Oct 15 '14 at 13:37
  • Its a original size, scaled down by applying transform in animation – Deekshith Bellare Oct 15 '14 at 13:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63113/discussion-between-boda-sira-and-kidsid49). – Deekshith Bellare Oct 15 '14 at 14:00
1

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]; 
}];
Community
  • 1
  • 1
Clay Garrett
  • 1,023
  • 8
  • 11