【问题标题】:Springboard-like animation for deleting UICollectionViewCells用于删除 UICollectionViewCells 的类似跳板的动画
【发布时间】:2014-09-10 15:46:39
【问题描述】:

我正在尝试找出一种方法来动画删除UICollectionViewCell,类似于 iOS 7 中的跳板。被删除的单元格应该缩小成虚无,剩余的单元格应该滑过以填充它的位置(滑动是重要部分)。我试过了:

• 将每个单元格的框架设置为前一个单元格的框架,如下所示:

UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
CGRect rect = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i - 1 inSection:0]].frame;      
[UIView animateWithDuration:0.2 animations:^{
    cell.frame = rect;
}];

但这不起作用,因为细胞根本没有移动。

• 以下两项:

[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];

[UIView animateWithDuration:0.2 animations:^{
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
}];

但这些会使细胞交叉淡化到所需的位置,而不是滑动。

我还找到了this project,它使用以下内容:

for (int i = index+1; i<[items count]; i++) {
    SEMenuItem *item = [items objectAtIndex:i];
    [UIView animateWithDuration:0.2 animations:^{

        if (i < index + remainingNumberOfItemsInPage) {

            int intVal = item.frame.origin.x;
            if (intVal %3== 0)
                [item setFrame:CGRectMake(item.frame.origin.x+2*item.frame.size.width, item.frame.origin.y-item.frame.size.height-5, item.frame.size.width, item.frame.size.height)];
            else
                [item setFrame:CGRectMake(item.frame.origin.x-item.frame.size.width, item.frame.origin.y, item.frame.size.width, item.frame.size.height)];
        }

        [item updateTag:item.tag-1];
    }];
}

但是,它不在UICollectionView 中,所以它对我没有帮助。

注意:我在 iPad 上执行此操作,以防万一对您很重要。

有什么想法吗?

【问题讨论】:

标签: ios objective-c ios7 uicollectionview springboard


【解决方案1】:

子类UICollectionViewFlowLayout。实施:

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexPath]; // might need to replace self with super

    attr.transform = CGAffineTransformMakeScale(0, 0);

    return attr;
}

【讨论】:

  • attr.transform = CGAffineTransformMakeScale(0, 0); 有效。另一个实际上并没有缩小单元格的大小。
【解决方案2】:

除了 duci9y 的回答,(使用attr.transform = CGAffineTransformMakeScale(0,0);),以下使瓷砖滑动。我不敢相信我以前没有想到这一点。

[self.collectionView performBatchUpdates:^{
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    [dataSource removeObjectAtIndex:indexPath.row];
} completion:nil];

【讨论】:

  • 这可能会导致动画错误。我建议您更改attrcenter 属性以与transform 一起离开屏幕以获得所需的效果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多