【问题标题】:UICollectionView : animation custom layoutUICollectionView : 动画自定义布局
【发布时间】:2012-10-11 13:25:09
【问题描述】:

我在 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];
}

有什么想法吗? 谢谢!

【问题讨论】:

    标签: ios animation layout uicollectionview


    【解决方案1】:

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

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

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

    所以,总结一下:

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

    【讨论】:

    • 这是给那些想看的人的结果 :) d.pr/v/QUTF(我有一个小故障问题)
    • 您需要增加堆栈中“顶部”项目的 z 索引,您可以在调整框架的相同位置执行此操作。
    • 我已经这样做了,但似乎问题出在其他地方。启用慢速动画:d.pr/v/oEm4
    • 我明白了。你应该问一个单独的问题。
    • @Pierre 链接d.pr/v/QUTF 似乎不起作用。你能分享你的实现吗?
    猜你喜欢
    • 2016-01-31
    • 2015-07-07
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多