简体   繁体   English

UICollectionView moveItemAtIndexPath:toIndexPath:不在屏幕上发出移动项目

[英]UICollectionView moveItemAtIndexPath:toIndexPath: issues moving items not on screen

I'm trying to animate a re-sort of items in UICollectionView on iOS 6. 我正在尝试在iOS 6上的UICollectionView中重新排序项目的动画。

I wrote this code: 我写了这段代码:

    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. 首先,我将项目的所有当前位置保存在字典中,然后我将项目分类到新位置,然后我将遍历所有项目,并将它们从旧位置移动到新位置。

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. 当所有项目都显示在屏幕上时,这很有用,但如果列表长度超过10,这使得某些项目当前不可见(因为它们位于列表的可见部分的上方或下方),则会导致这些项目突然弹出存在于可见索引中并动画到其他地方,当动画停止时它们被隐藏。 This looks really bizarre as there are items that appear on top of others... 这看起来很奇怪,因为有些项目出现在其他人之上......

Is this an issue with iOS 6 UICollectionView, and am I doing something wrong here? 这是iOS 6 UICollectionView的问题,我在这里做错了吗?

It looks like UICollectionView is reusing cells in such a way as to make your animation look bad. 看起来UICollectionView正在重复使用单元格,以使您的动画看起来很糟糕。 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. 为了使这些动画看起来不错,您还需要更新数据源,因为代码使用它来确定单元格在动画期间应该在哪里开始和结束。

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. 通过这种方式,Apple的代码可以提供所需的信息,以便以更美观的方式移动您的单元格。

From the Apple documentation on UICollectionViews : UICollectionViews上Apple文档

To insert, delete, or move a single section or item, you must follow these steps: 要插入,删除或移动单个部分或项目,您必须执行以下步骤:

  1. Update the data in your data source object. 更新数据源对象中的数据。
  2. Call the appropriate method of the collection view to insert or delete the section or item. 调用集合视图的相应方法以插入或删除节或项。

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. 如果没有,集合视图可能会从您的数据源收到错误的项目集,或者询问不存在的项目并使您的应用程序崩溃。

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

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