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

Ios 如何在布局转换期间设置UICollectionViewCells边框宽度/颜色的动画?,ios,animation,uicollectionview,Ios,Animation,Uicollectionview,我有一个UICollectionView,它使用了UICollectionViewFlowLayout的自定义子类。这又使用了一个自定义子类uiCollectionViewLayoutAttribute,其属性会影响单元格上的边框颜色和厚度等内容。在集合视图上执行动画布局更改时,如何在动画中包含这些内容 实施详情: 假设在MyLayoutAttributes中,我有一个枚举属性LayoutType,其值为TypeBig和TypeSmall。我有一个单元格类MyCell,其中有一个UILabel作

我有一个
UICollectionView
,它使用了
UICollectionViewFlowLayout
的自定义子类。这又使用了一个自定义子类
uiCollectionViewLayoutAttribute
,其属性会影响单元格上的边框颜色和厚度等内容。在集合视图上执行动画布局更改时,如何在动画中包含这些内容

实施详情:

假设在
MyLayoutAttributes
中,我有一个枚举属性
LayoutType
,其值为
TypeBig
TypeSmall
。我有一个单元格类
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;
}
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"];

当集合视图的布局发生更改时(使用
[collectionView setCollectionViewLayout:animated:
),单元格大小和位置的更改将按预期设置动画,但边框未设置动画。

,对层特性的更改不会被
UIView
上的动画方法捕获,就像
animateWithDuration
一样。因此,必须使用
CAAnimation
将它们添加到层中。因此,在
applyLayoutAttributes
中,我做了如下操作:

-(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;
}
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"];
感谢您提供了有关获得正确动画持续时间的技巧