UICollectionView:动画自定义布局 [英] UICollectionView : animation custom layout

查看:424
本文介绍了UICollectionView:动画自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UICollectionView显示大量图像的细胞。
随着一个按钮,我想能够在组第一个我的所有细胞。

I'm displaying lots of image cells in an UICollectionView. With one button I would like to be able to group all my cell over the first one.

这是运作良好,但是当我试图动画过渡添加到我的重新组合动作,什么都不会发生。

This is working well but when I'm trying to add an animation transition to my regroup action, nothing happens.

在这里,我在一个自定义布局使用的方法:

Here the method I use in a custom layout :

- (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];
}

你知道吗?
谢谢!

Any idea ? Thanks !

推荐答案

动画将不会从您致电它们的方法内工作。

Animations won't work from within the method you are calling them from.

要更改布局和动画到一个新的,最简单的方法是调用 performBatchUpdates 在您的收藏观,具有每个块的参数。这种无效的布局和动画到新一个给你。

To change the layout and animate to a new one, the simplest method is to call performBatchUpdates on your collection view, with nil for each block parameter. This invalidates your layout and animates to the new one for you.

在此之前,你会告诉你希望发生新的布局布局对象。此外,在 layoutAttributesForElementsInRect ,只需检查你的布尔变量和分组框应用(可能中间会更好),以所有属性,你现在正在做,但没有动画。您还需要重现 layoutAttributesForElementAtIndexPath 这code。

Before doing this, you'd tell the layout object that you want the new layout to occur. Also, inside layoutAttributesForElementsInRect, simply check your Boolean variable and apply the grouped frame (probably center would be better) to all attributes as you are doing now, but without animation. You'll also need to reproduce this code in layoutAttributesForElementAtIndexPath.

因此​​,简言之:


  1. 删除从那里您的布局无效电话

  2. 删除从他们在哪里,只是修改布局属性的动画调用

  3. 添加..forElementAtIndexPath code,以及

  4. 在您的视图控制器,调用布局对象的重新组合的方法,然后调用集合视图performBatchupdates。

这篇关于UICollectionView:动画自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆