我试图设置持续时间(使用我会尝试的普通方法)来设置 UICollectionView 的selectItemAtIndexPath:animated:scrollPosition:
方法(或scrollToItemAtIndexPath:atScrollPosition:animated:
方法)的动画持续时间。我试过[UIView setAnimationDuration]
了,我试过把它包装在一个CATransaction
. 到目前为止,我在更改动画持续时间方面一直没有成功(尽管我承认我可能在这个逻辑上犯了一个错误)。
想法?
更新:
我在这里尝试了很多方法。最接近的解决方案是做我们通常对UIScrollView
动画所做的事情(通过设置动画:参数NO
并将其包装在UIView
动画块中)。这对于滚动视图非常有效。UICollectionView
但是,由于某种原因,这与创建过程有关。
我在下面包含了一个使用两种方法的示例。每种方法都假设您有 4 个部分,每个部分包含 4 个项目。此外,动画假定您从 0,0 移动到 3,3。
使用默认动画
这里的部分问题当然似乎与UICollectionView
. 如果您采用以下方法(使用默认动画选项) - 一切正常:
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:3]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
执行此操作时,将创建当前可见单元格和目标单元格之间的每个单元格。我已包括登录该collectionView:cellForItemAtIndexPath:
方法:
2013-05-18 09:33:24.366 DEF-CV-Testing[75463:c07] Transition
Cell Created for Index Path: <NSIndexPath 0x8913f40> 2 indexes [0, 1]
Cell Created for Index Path: <NSIndexPath 0x75112e0> 2 indexes [0, 2]
Cell Created for Index Path: <NSIndexPath 0xfe1a6c0> 2 indexes [0, 3]
Cell Created for Index Path: <NSIndexPath 0x89159e0> 2 indexes [1, 0]
Cell Created for Index Path: <NSIndexPath 0x8a10e70> 2 indexes [1, 1]
Cell Created for Index Path: <NSIndexPath 0x7510d90> 2 indexes [1, 2]
Cell Created for Index Path: <NSIndexPath 0x75112a0> 2 indexes [1, 3]
Cell Created for Index Path: <NSIndexPath 0x8915a00> 2 indexes [2, 0]
Cell Created for Index Path: <NSIndexPath 0x75111c0> 2 indexes [2, 1]
Cell Created for Index Path: <NSIndexPath 0xfe17f30> 2 indexes [2, 2]
Cell Created for Index Path: <NSIndexPath 0xfe190c0> 2 indexes [2, 3]
Cell Created for Index Path: <NSIndexPath 0xfe16920> 2 indexes [3, 0]
Cell Created for Index Path: <NSIndexPath 0x75112a0> 2 indexes [3, 1]
Cell Created for Index Path: <NSIndexPath 0xfe1a4f0> 2 indexes [3, 2]
Cell Created for Index Path: <NSIndexPath 0x75142d0> 2 indexes [3, 3]
使用自定义动画
将scrollToItemAtIndexPath:
方法包装在UIView
动画块中时,无法正确创建项目。在此处查看代码示例:
[UIView animateWithDuration:5.0 delay:0.0 options:0 animations:^{
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:3]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:NO];
} completion:^(BOOL finished) {
NSLog(@"Completed");
}];
当前可见的单元格消失,只创建目标单元格。我在该collectionView:cellForItemAtIndexPath:
方法中包含了相同的日志记录:
Transition
Cell Created for Index Path: <NSIndexPath 0x71702f0> 2 indexes [3, 3]
Completed