UICollectionView moveItemAtIndexPath:toIndexPath: 问题移动项目不在屏幕上 [英] UICollectionView moveItemAtIndexPath:toIndexPath: issues moving items not on screen

查看:14
本文介绍了UICollectionView moveItemAtIndexPath:toIndexPath: 问题移动项目不在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 6 上的 UICollectionView 中为重新排序的项目设置动画.

I'm trying to animate a re-sort of items in UICollectionView on iOS 6.

我写了这段代码:

    NSMutableDictionary *from = [NSMutableDictionary dictionaryWithCapacity:self.count];
    for (int ii = 0; ii < self.count; ii++) {
        MyItem item = [self getItemAtIndex:(NSInteger)ii];
        [from setObject:[NSNumber numberWithInt:ii] forKey:[NSNumber numberWithInt:item.id]];
    }

    [self sort];

    [self.collectionView performBatchUpdates:^{
        for (int ii = 0; ii < self.count; ii++) {
            MyItem item = [self getItemAtIndex:(NSInteger)ii];
            NSNumber *prevPos = (NSNumber *)[from objectForKey:[NSNumber numberWithInt:item.id]];
            if ([prevPos intValue] != ii) {
                NSIndexPath *from = [NSIndexPath indexPathForItem:prevPos.intValue inSection:0];
                NSIndexPath *to = [NSIndexPath indexPathForItem:ii inSection:0];
                [self.collectionView moveItemAtIndexPath:from toIndexPath:to];
            }
        }
    } completion:nil];

首先,我将项目的所有当前位置保存在字典中,然后将项目排序到新位置,然后检查所有项目,并将它们从旧位置移动到新的.

So first, I'm saving all the current locations of the items in a dictionary, then I'm sorting the items into new positions, then I'm going over all items, and moving them from the old position to the new one.

当所有项目都显示在屏幕上时,这很有效,但如果列表长于 10,这会使某些项目当前不可见(因为它们位于列表的可见部分之上或之下),则会导致这些项目突然出现在可见索引中并在其他地方设置动画,当动画停止时它们被隐藏.这看起来真的很奇怪,因为有些项目出现在其他项目之上......

This works great when all items are displayed on the screen, but if the the list if longer then 10 which makes some items not currently visible (since they are above or below the visible section of the list), it causes these items to suddenly pop into existence in visible indexes and get animated into other places, and when the animation stops they are hidden. This looks really bizarre as there are items that appear on top of others...

这是 iOS 6 UICollectionView 的问题,我在这里做错了吗?

Is this an issue with iOS 6 UICollectionView, and am I doing something wrong here?

推荐答案

看起来 UICollectionView 正在重用单元格以使您的动画看起来很糟糕.任何离开屏幕的单元格都可以重复使用.

It looks like UICollectionView is reusing cells in such a way as to make your animation look bad. Any cells that go offscreen become available for reuse.

那么为什么你的动画看起来很时髦?

So why is your animation looking funky?

当您移动单元格时,单元格的开始和结束位置以及它是否被隐藏都会自动处理.为了让这些动画看起来不错,您还需要更新数据源,因为代码似乎使用它来确定动画期间单元格的开始和结束位置.

The starting and ending location of a cell, and if it gets hidden or not, is all taken care of automatically when you move cells around. For those animations to look good, you need to also update your data source, as it appears the code uses that to determine where cells should start and end during the animation.

在调用索引更新块之前,请确保您的数据源已使用新的索引位置进行更新.这样一来,Apple 的代码就拥有了以更美观的方式移动您的单元格所需的信息.

Before you call your index update block, make sure your data source is updated with the new index locations. That way Apple's code has the information it needs to move your cells around in a more, lets say, aesthetically pleasing manner.

来自 关于 UICollectionViews 的 Apple 文档:

要插入、删除或移动单个部分或项目,您必须按照以下步骤操作:

To insert, delete, or move a single section or item, you must follow these steps:

  1. 更新数据源对象中的数据.
  2. 调用集合视图的适当方法来插入或删除部分或项目.

在通知集合视图任何更改之前更新数据源至关重要.集合视图方法假定您的数据源包含当前正确的数据.如果没有,collection view 可能会从您的数据源接收到错误的项目集,或者请求不存在的项目并导致您的应用崩溃.

It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app.

这篇关于UICollectionView moveItemAtIndexPath:toIndexPath: 问题移动项目不在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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