繁体   English   中英

UICollectionView:动画自定义布局

[英]UICollectionView : animation custom layout

我在UICollectionView中显示了很多图像单元格。 只需一个按钮,我就可以将所有细胞分组到第一个细胞上。

这很好用但是当我试图将动画过渡添加到我的重新组合动作时,没有任何反应。

这是我在自定义布局中使用的方法:

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];

    if([allAttributesInRect count] > 0 && _isRegroup)
    {
        UICollectionViewLayoutAttributes *firstAttribute = [allAttributesInRect objectAtIndex:0];
        CGRect frame = firstAttribute.frame;

        for(UICollectionViewLayoutAttributes *attribute in allAttributesInRect)
            [UIView animateWithDuration:0.3f animations:^{attribute.frame = frame;}];
    }
    return allAttributesInRect;
}

- (void)regroupCells:(BOOL)isRegroup // This method is called from my collection controller when my button is pressed
{
    _isRegroup = isRegroup;
    [self invalidateLayout];
}

任何想法 ? 谢谢 !

动画将无法在您调用它们的方法中起作用。

要更改布局并将动画更改为新布局,最简单的方法是在集合视图上调用performBatchUpdates ,每个块参数都使用nil 这会使您的布局和动画无效。

在执行此操作之前,您将告诉布局对象您希望新布局发生。 此外,在layoutAttributesForElementsInRect ,只需检查您的布尔变量并将分组的帧(可能是中心会更好)应用于您现在正在执行的所有属性,但没有动画。 您还需要在layoutAttributesForElementAtIndexPath重现此代码。

所以,总结一下:

  1. 从您所在的位置删除无效的布局调用
  2. 从它们所在的位置删除动画调用,只需修改布局属性即可
  3. 添加..forElementAtIndexPath代码
  4. 在视图控制器中,在布局对象上调用regroup方法,然后在集合视图上调用performBatchupdates。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM