gpt4 book ai didi

iphone - UICollectionView selectItemAtIndexPath 的动画持续时间 :animated:scrollPosition:

转载 作者:行者123 更新时间:2023-12-03 18:14:16 25 4
gpt4 key购买 nike

我尝试设置持续时间(使用我会尝试的正常方法)来设置 UICollectionView 的 selectItemAtIndexPath:animated:scrollPosition: 方法(或 scrollToItemAtIndexPath:atScrollPosition)的动画持续时间:animated: 方法)。我尝试过 [UIView setAnimationDuration],并尝试将其包装在 CATransaction 中。到目前为止,我在更​​改动画持续时间方面一直不成功(尽管我承认我可能在这个逻辑上犯了错误)。

想法?

更新:

我在这里尝试了很多方法。最接近的解决方案是执行我们通常对 UIScrollView 动画执行的操作(通过将animated: 参数设置为 NO 并将其包装在 UIView 中)动画 block )。这对于 ScrollView 来说效果非常好。然而,由于某种原因,这与 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]

使用自定义动画

UIView 动画 block 中包装 scrollToItemAtIndexPath: 方法时,无法正确创建项目。请参阅此处的代码示例:

[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

最佳答案

我在 UITableView 上遇到了类似的问题,我通过以下代码解决了这个问题:

[CATransaction begin];
[CATransaction setCompletionBlock:onCompletion];
[CATransaction setAnimationDuration:duration];

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:3]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];

[CATransaction commit];

显然,你不能用animated:NO来调用它,因为方法内的动画代码很重要。使用 CATransaction 来包装动画对我有用。

关于iphone - UICollectionView selectItemAtIndexPath 的动画持续时间 :animated:scrollPosition:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com