selectItemAtIndexPath:animated:scrollPosition:
UICollectionView のメソッド (またはメソッド)のアニメーション期間を設定するために、期間を設定しようとしました (通常の方法で試しました) scrollToItemAtIndexPath:atScrollPosition:animated:
。 を試し[UIView setAnimationDuration]
、 でラップしてみましたCATransaction
。 これまでのところ、そのアニメーション期間を変更することはできませんでした (ただし、このロジックで間違いを犯した可能性は認めます)。
ご意見は?
アップデート:
私はここで多くのアプローチを試しました。最も近い解決策は、UIScrollView
アニメーションで通常行うことを行うことです (animated: 引数を設定してNO
、それをアニメーション ブロックでラップするUIView
)。これはスクロール ビューでは完全に正常に機能します。ただし、UICollectionView
何らかの理由で、作成プロセスが台無しになります。
以下に 2 つのアプローチを使用した例を示します。各アプローチでは、セクションが 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
ベストアンサー1
私も同様の問題を抱えていましたが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
アニメーションをラップするために を使用してもうまくいきました。