如何在调用deleteItemsAtIndexPaths后维护其他UICollectionView单元的状态?

时间:2014-10-11 19:22:36

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

as" deleteItemsAtIndexPaths"文件说

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

在我的情况下,当从集合视图中删除一个单元格时,它的所有单元格都在做一些动画但是一旦我调用了deleteItemsAtIndexPaths,那么移动到新位置的旧动画单元格会停止执行该动画吗?

所以我的问题是,由于deleteItemsAtIndexPaths将细胞移动到新位置后如何保持状态相同?

编辑: - 我的所有单元格都按比例缩小并且与#34相似;当用户长按应用程序图标以删除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) {

        }];
    }
}];

但是当我尝试使用

删除一个单元格时
 [cardsViewController.collectionView deleteItemsAtIndexPaths:indexPathArray];

所有移动的单元格 - (删除单元格后的单元格)停止进行摇摆并按比例放大。他们不保留相同的行为意味着动画。

例如。样品gif在这里。 enter image description here

2 个答案:

答案 0 :(得分:4)

如果我理解你的要求是正确的 a)缩小和摆动
enter image description here
b)关于删除和重新排列,不要按比例放大/缩小但继续摆动
enter image description here

将这些方法添加到您的单元格

-(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) {

                     }];


}

现在进行第1步:为每个可见单元调用scaleDownAndWoble  Step2:删除动画完成后调用所有可见单元格的摆动

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

                                      }];
    }

答案 1 :(得分:1)

这个答案可以帮到你吗?

Avoid animation of UICollectionView after reloadItemsAtIndexPaths

看起来像是为UICollection视图禁用某些动画的方法(修改了答案中的代码以适合您的问题):

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