【发布时间】:2013-05-16 20:58:11
【问题描述】:
我已尝试设置持续时间(使用我会尝试的常规方法)以设置 UICollectionView 的 selectItemAtIndexPath:animated:scrollPosition: 方法(或 scrollToItemAtIndexPath:atScrollPosition:animated: 方法)的动画持续时间。我尝试过[UIView setAnimationDuration],并尝试将其包装在CATransaction 中。到目前为止,我在更改动画持续时间方面一直没有成功(尽管我承认我可能在这个逻辑上犯了一个错误)。
想法?
更新:
我在这里尝试了很多方法。最接近的解决方案是执行我们通常为 UIScrollView 动画所做的事情(通过将 animated: 参数设置为 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
【问题讨论】:
-
您是否尝试将动画设置为 NO,然后将其包裹在
UIView animateWithDuration中?这就是我为我的滚动视图动画之一做的事情。我做[UIView animtateWithDuration ... [scrollView scrollToRect.. -
是的。不幸的是,这搞砸了单元格的创建。它滚动 - 但只显示动画中的第一个和最后一个 UICollectionViewsCell。
-
只是出于好奇,您是否也尝试过暴力破解您的块中的动画。
[UIView animateWithDuration:.... animations:^{ [self.colectionView scrollToItemAtIndexPath:[0, 1]; self.collectionView.scrollToItemAtIndexpath[0,2];....self.collectionView.scrollToItemAtIndexPath[3,3]; -
似乎没有任何计时器等的唯一方法是调用
objc_msgSend(self.collectionView, @selector(_setContentOffsetAnimationDuration:), 10.0),其中_setContentOffsetAnimationDuration:似乎是私有API的一部分...... -
虽然这不是一个答案,但它至少可以为您的第二种情况提供一些启示:您观察到的是优化工作。如果 iOS 无论如何都不会显示它们,为什么 应该 创建中间单元格(动画:否)。因此,您只需创建最后一个单元格并切换到该单元格 - 记住视图不知道您正在“从外部”为其设置动画;)
标签: iphone core-animation uicollectionview