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 上是否有属性,或者有更好更标准的方法来做这样的事情。谢谢。

4

2 回答 2

1

我会创建一个 UICollectionViewLayout 子类。添加一个名为editing 的BOOL 属性。编辑更改时,调用 invalidateLayout。然后在 -layoutAttributesForItemAtIndexPath: 方法返回的属性中,您可以指定一个变换。

您的方法的问题在于它仅影响可见单元格。UICollectionViewLayout 子类很好,因为即使添加了新单元格,它也会将转换应用于所有单元格。并且它将所有集合视图布局处理移出视图控制器。

单元格属性可以包括 frame、size、center、transform(3D)、alpha 和您自己的自定义属性。

您将按照 wL_ 的建议更改 -performBatchUpdates: 块中的编辑值。

- (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 变换。仿射变换就足够了。

于 2014-03-22T17:08:33.290 回答
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:^{}];
于 2013-07-31T23:43:11.577 回答