Ios 对选定的uicollectionview单元格设置动画

Ios 对选定的uicollectionview单元格设置动画,ios,ios6,uicollectionview,uicollectionviewcell,Ios,Ios6,Uicollectionview,Uicollectionviewcell,我已经创建了一个customlayout,并在layoutAttributesForItemAtIndexPath中设置了我的单元格位置属性,如下所示 attributes.center = CGPointMake((size.width/2 - 100) + 100, (size.height/2 - 150) +100); 我想在选定单元格时设置其动画。复制我们通过initialLayoutAttributesForAppearingItemAtIndexPath和FinallYout

我已经创建了一个customlayout,并在
layoutAttributesForItemAtIndexPath
中设置了我的单元格位置属性,如下所示

attributes.center = CGPointMake((size.width/2 - 100) + 100, (size.height/2 - 150) +100);
 
我想在选定单元格时设置其动画。复制我们通过
initialLayoutAttributesForAppearingItemAtIndexPath
FinallYoutAttributesforIsAppearinGiteMatIndexPath
获得的动画类型

我希望在选中和取消选中单元格时执行此操作

例如:


单元格A位于位置
0,0
。单元格B位于位置
50100
。如果选择单元格B,我希望将其设置为
0,0
。同时将单元格A设置为
50100
。基本上是切换位置,但设置了动画。

我正在使用UICollectionViewDelegate中的didSelectItemAtIndexPath设置属性动画:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_collectionView.collectionViewLayout invalidateLayout];
    UICollectionViewLayoutAttributes *newAttributes = [_collectionView layoutAttributesForItemAtIndexPath:indexPath];

    //use new attributes for animation
}

也许是另一种方法。只需覆盖collectionViewCell中的isSelected

 override var isHighlighted: Bool {
    didSet {
        if isHighlighted {
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
                // animate highlight
            }, completion: nil)
        } else {
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
                // animate unHighligh
            }, completion: nil)
        }
    }
}
在这篇文章中,您可以看到一个关于如何设置大小更改动画的示例