2012-12-03 36 views
5

我想知道在UICollectionView中動畫所有單元格的好方法是什麼。我試圖在UICollectionView中模擬編輯。所以我想要做的就是收縮UICollectionViewCells的所有邊界。所以,我有什麼是這樣的:動畫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的屬性,或做這樣的事情更好更標準的方式。謝謝。

回答

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:^{}]; 
1

我將創建一個UICollectionViewLayout子類。添加一個名爲editing的BOOL屬性。編輯更改時,請調用invalidateLayout。然後在由-layoutAttributesForItemAtIndexPath:方法返回的屬性中,您可以指定一個變換。

您的方法存在的問題是它隻影響可見細胞。 UICollectionViewLayout子類很好,因爲即使在添加新元素時,它也會將變換應用於所有單元格。它將視圖控制器的所有集合視圖佈局處理移出。

單元屬性可以包括框架,大小,中心,變換(3D),alpha和您自己的自定義屬性。

您將更改-performBatchUpdates:block中的編輯值,如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變換。仿射變換就足夠了。

相關問題