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