Skip to content

Collection View动画

戴铭 edited this page Mar 4, 2015 · 1 revision

Collection View 布局动画

插入删除元素

  • 计算cells的动画,collection view 需通过initialLayoutAttributesForAppearingItemAtIndexPath:和finalLayoutAttributesForAppearingItemAtIndexPath:方法询问布局对象,获得最初和最后的属性。下面是实现cells从屏幕底部发射并且旋转飞到对应位置
- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
     UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
     attr.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.2, 0.2), M_PI);
     attr.center = CGPointMake(CGRectGetMidX(self.collectionView.bounds), CGRectGetMaxY(self.collectionView.bounds));
     return attr;
}

finalLayoutAttributesForAppearingItemAtIndexPath: 方法中,除了设定了不同的 transform 以外,其他都很相似。

UICollectionViewLayoutAttributes的方法返回的属性

  • prepareLayout
  • prepareForCollectionViewUpdates:
  • finalizeCollectionViewUpdates
  • prepareForAnimatedBoundsChange:
  • finalizeAnimatedBoundsChange
  • shouldInvalidateLayoutForBoundsChange:
Clone this wiki locally