在UICollectionView中动画化所有UICollectionViewCell

5

我想知道一种好的方法来为UICollectionView中的所有单元格添加动画。我正在尝试模拟对UICollectionView进行编辑的过程。因此,我想做的是缩小UICollectionViewCells的边界。我的代码如下:

- (IBAction)startEditingMode:(id)sender {
    [_items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

        [UIView animateWithDuration:0.25 animations:^{
            cell.layer.transform = CATransform3DMakeScale(0.9, 0.9, 1);
        }];
    }];
}

它可以工作,但我不确定UICollectionView上是否有属性,或者更好的更标准的方法来做这样的事情。谢谢。

2个回答

1
我会创建一个UICollectionViewLayout子类。添加一个名为editing的BOOL属性。当editing更改时,调用invalidateLayout方法。然后在-layoutAttributesForItemAtIndexPath:方法返回的属性中,您可以指定一个变换。
你的方法存在问题,它只影响可见单元格。UICollectionViewLayout子类很好,因为它将应用转换到所有单元格,即使新单元格被添加也是如此。而且它将所有集合视图布局处理移出了视图控制器。
单元格属性可以包括frame、size、center、transform(3D)、alpha和您自己的自定义属性。
您将通过-performBatchUpdates:块更改编辑值,如wL_所建议的那样。
- (IBAction)startEditingMode:(id)sender {
    [self.collectionView performBatchUpdates:^{
        ((MyCollectionViewLayout *)self.collectionView.collectionViewLayout).editing = YES;
    }
    completion:NULL];
}

在UICollectionViewLayout子类中:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    if (self.editing) {
        attributes.transform = CGAffineTransformMakeScale(0.9, 0.9);
    }
    else {
        attributes.transform = CGAffineTransformIdentity;
    }

    return attributes;
}

请注意,这里可能不需要3D变换。仿射变换已经足够。


0

你尝试过使用 UICollectionView performBatchUpdates: 吗?

就像这样:

[collectionView performBatchUpdates:^{
    [_items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        cell.layer.transform = CATransform3DMakeScale(0.9, 0.9, 1);
    }];
} completion:^{}];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接