njk*_*mer 18 ios6 uicollectionview
有没有人看到这个问题的正确答案?
initialLayoutAttributesForAppearingItemAtIndexPath似乎所有可见细胞都被调用,而不仅仅是被插入的细胞.根据Apple自己的文档:
对于移动的项目,集合视图使用标准方法来检索项目的更新布局属性.对于要插入或删除的项目,集合视图会调用一些不同的方法,您应该覆盖这些方法以提供适当的布局信息
这听起来不像正在发生的事情......其他单元格没有被插入,它们正被移动,但它正在呼唤initialLayoutAttributesForAppearingItemAtIndexPath那些被移动的单元格.
我已经看到使用工作prepareForCollectionViewUpdates:来跟踪哪些indexPaths正在更新并且只更改那些,但这似乎有点奇怪,它将再次成为他们自己的文档.有没有其他人找到更好的解决方法?
Dan*_*mov 23
我发现Mark Pospesel撰写的这篇博文有用.
作者还修复了WWDC CircleLayout样本并将其发布在Github上.
感兴趣的方法:
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
// Keep track of insert and delete index paths
[super prepareForCollectionViewUpdates:updateItems];
self.deleteIndexPaths = [NSMutableArray array];
self.insertIndexPaths = [NSMutableArray array];
for (UICollectionViewUpdateItem *update in updateItems)
{
if (update.updateAction == UICollectionUpdateActionDelete)
{
[self.deleteIndexPaths addObject:update.indexPathBeforeUpdate];
}
else if (update.updateAction == UICollectionUpdateActionInsert)
{
[self.insertIndexPaths addObject:update.indexPathAfterUpdate];
}
}
}
- (void)finalizeCollectionViewUpdates
{
[super finalizeCollectionViewUpdates];
// release the insert and delete index paths
self.deleteIndexPaths = nil;
self.insertIndexPaths = nil;
}
// Note: name of method changed
// Also this gets called for all visible cells (not just the inserted ones) and
// even gets called when deleting cells!
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
// Must call super
UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
if ([self.insertIndexPaths containsObject:itemIndexPath])
{
// only change attributes on inserted cells
if (!attributes)
attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
attributes.alpha = 0.0;
attributes.center = CGPointMake(_center.x, _center.y);
}
return attributes;
}
// Note: name of method changed
// Also this gets called for all visible cells (not just the deleted ones) and
// even gets called when inserting cells!
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
// So far, calling super hasn't been strictly necessary here, but leaving it in
// for good measure
UICollectionViewLayoutAttributes *attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
if ([self.deleteIndexPaths containsObject:itemIndexPath])
{
// only change attributes on deleted cells
if (!attributes)
attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
// Configure attributes ...
attributes.alpha = 0.0;
attributes.center = CGPointMake(_center.x, _center.y);
attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
}
return attributes;
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*eau 11
你不是一个人.UICollectionViewLayout头文件注释使事情更清晰一些.
对于失效前屏幕上的每个元素,将调用finalLayoutAttributesForDisappearingXXX,并从屏幕上的内容到最终属性进行动画设置.
对于失效后屏幕上的每个元素,initialLayoutAttributesForAppearingXXX将被称为从这些初始属性到最终在屏幕上的动画设置.
基本上finalLayoutAttributesForDisappearingItemAtIndexPath在动画块开始之前为屏幕上的initialLayoutAttributesForAppearingItemAtIndexPath每个项目调用,并在动画块结束后为每个项目调用.由您来缓存发送的UICollectionViewUpdateItem对象数组,UICollectionViewUpdateItem以便您知道如何设置初始和最终属性.在我的情况下,我缓存了以前的布局矩形,prepareForCollectionViewUpdates所以我知道要使用正确的初始位置.
让我感到困惑的一件事是你应该使用super的实现prepareLayout并修改它返回的属性.我只是initialLayoutAttributesForAppearingItemAtIndexPath在调用我的实现,动画不起作用,因为布局位置不同.
| 归档时间: |
|
| 查看次数: |
8156 次 |
| 最近记录: |