performBatchUpdates上的UICollectionView性能问题

Kev*_*fer 7 performance ios ios6 uicollectionview

我们正在尝试使用自定义布局设置UICollectionView.每个CollectionViewCell的内容都是一个图像.总之,将会有数千张图像,并且在某个特定时间可以看到大约140-150张图像.在动作事件中,可能会在位置和大小上重新组织所有单元格.目标是使用performBatchUpdates方法为当前所有移动事件设置动画.在一切变得动画之前,这会导致很长的延迟时间.

到目前为止,我们发现内部方法layoutAttributesForItemAtIndexPath是为每个单元格调用的(总共几千个).另外,调用方法cellForItemAtIndexPath以获得比实际可以在屏幕上显示的更多的单元格.

有没有可能提高动画的性能?


默认的UICollectionViewFlowLayout无法真正提供我们想要在应用程序中实现的那种设计.这是我们的一些代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance]; //Singleton holding some global information
NSArray *plistArray = dm.plistArray; //Array containing the contents of the cells
NSDictionary *dic = plistArray[[indexPath item]];
RPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
cell.layer.borderColor = nil;
cell.layer.borderWidth = 0.0f;
[cell loadAndSetImageInBackgroundWithLocalFilePath:dic[@"path"]]; //custom method realizing asynchronous loading of the image inside of each cell
return cell;
}
Run Code Online (Sandbox Code Playgroud)

layoutAttributesForElementsInRect遍历所有元素,为rect中的所有元素设置layoutAttributes.for语句在第一个单元格中断,超过了rect右下角定义的边框:

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray* attributes = [NSMutableArray array];
RPDataModel *dm = [RPDataModel sharedInstance];
for (int i = 0; i < dm.cellCount; i++) {
    CGRect cellRect = [self.rp getCell:i]; //self.rp = custom object offering methods to get information about cells; the getCell method returns the rect of a single cell
    if (CGRectIntersectsRect(rect, cellRect)) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[dm.relevanceArray[i][@"product"] intValue] inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attribute.size = cellRect.size;
        attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width / 2, cellRect.origin.y + attribute.size.height / 2);
        [attributes addObject:attribute];
    } else if (cellRect.origin.x > rect.origin.x + rect.size.width && cellRect.origin.y > rect.origin.y + rect.size.height) {
        break;
    }
}
return attributes;
}
Run Code Online (Sandbox Code Playgroud)

在布局更改时,无论layoutAttributesForElementsInRect中定义的单元格数量是否有限,结果都几乎相同.如果系统中的所有单元格不受限制,或者它调用了layoutAttributesForElementAtIndexPath方法,用于所有丢失的单元格,如果它是有限的.总体而言,每个单元格的属性都以某种方式被使用.

-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance];
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect cellRect = [self.rp getCell:[dm.indexDictionary[@(indexPath.item)] intValue]];
attribute.size = cellRect.size;
attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width / 2, cellRect.origin.y + attribute.size.height / 2);
return attribute;
}    
Run Code Online (Sandbox Code Playgroud)

Rob*_*uss 4

在没有看到代码的情况下,我的猜测是您的layoutAttributesForElementsInRect方法正在迭代集合中的所有项目,这反过来又导致其他方法被过度调用。layoutAttributesForElementsInRect 正在给您一个提示 - 即它传递给您的 CGRect - 关于您需要根据屏幕上的内容调用layoutAttributesForItemAtIndexPath 的项目。

因此,这可能是性能问题的一部分 - 也就是说,调整您的自定义布局,以便它能够智能地了解正在更新的项目。

其他问题一般与动画性能有关。需要注意的一件事是是否正在进行任何类型的合成 - 确保您的图像是不透明的。另一件事是,如果您在图像上使用阴影,那么制作动画的成本可能会很高。提高阴影动画性能的一种方法是在调整图像大小时设置 ShadowPath 值 - 如果您确实有阴影,请告诉我并发布一些执行此操作的代码。