如何在布局转换期间为UICollectionViewCells的边框宽度/颜色设置动画?

Tom*_*ing 2 animation ios uicollectionview

我有一个UICollectionView使用自定义子类UICollectionViewFlowLayout.这反过来使用UICollectionViewLayoutAttributes具有属性的自定义子类,这些属性会影响单元格上的边框颜色和粗细.在我的集合视图上执行动画布局更改时,如何在动画中包含这些内容?

实施细节:

说,在MyLayoutAttributes我有一个枚举属性LayoutType与价值TypeBigTypeSmall.我有一个MyCell带有UILabel子视图的单元格类.在那个单元类中,我做了这样的事情:

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attr
{
  [super applyLayoutAttributes:attr];
  MyLayoutAttributes *myAttr = (MyLayoutAttributes *)attr;
  if (myAttr.layoutType == TypeSmall)
    self.layer.borderWidth = 1; //there's already a color set
  else
    self.layer.borderWidth = 0;
}
Run Code Online (Sandbox Code Playgroud)

当集合视图的布局更改(使用[collectionView setCollectionViewLayout:animated:])时,单元格大小和位置更改将按预期进行动画处理,但边框不会.

Tom*_*ing 6

事实证明,对图层属性的更改不会被UIView类似的动画方法捕获animateWithDuration.因此必须使用它们将它们添加到图层中CAAnimation.所以,在applyLayoutAttributes,我做这样的事情:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"borderColor"];
anim.fromValue = (id)[UIColor clearColor].CGColor;
anim.toValue = (id)[UIColor lightGrayColor].CGColor;
self.layer.borderColor = [UIColor lightGrayColor].CGColor;
anim.duration = [CATransaction animationDuration];
anim.timingFunction = [CATransaction animationTimingFunction];
[self.layer addAnimation:anim forKey:@"myAnimation"];
Run Code Online (Sandbox Code Playgroud)

感谢有关获得正确动画持续时间的技巧的答案.