![](/img/trans.png)
[英]Using UICollectionView scrollToItemAtIndexPath to return to last selected cell
[英]UICollectionView scrollToItemAtIndexPath cell disappears when half hidden
我正在嘗試自動移動UICollectionView
單元格。 移動它的代碼:
- (void)scroll {
row += 2;
[UIView animateWithDuration:1.f
delay:0.f
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
completion:^(BOOL finished) {
}];
}
我的單元格以2行模式水平組織:
|cell||cell||cell|->etc
|cell||cell||cell|->etc
問題在於,當滾動移動到下一個單元格時,整個collectionView都會移動,上一個單元格會消失。 恰好在屏幕上看不到上一個單元格的一半時發生。 為什么會發生? 任何想法如何解決該問題?
看起來像:(〜表示屏幕)
step 1:
~|cell||cell||cell|~
~|cell||cell||cell|~
step 2:
~ell||cell||cell||c~
~ell||cell||cell||c~
step 3:
~ll||cell||cell||ce~
~ll||cell||cell||ce~
step 4:
~ |cell||cell||cel~ --> here first cell disappears even though
~ |cell||cell||cel~ --> there is enough space for it
更重要的是,在單元格只有一半可見時(因此恰好在步驟3和4之間),將調用委托: collectionView:didEndDisplayingCell:forItemAtIndexPath:
好的,結果是無法對此進行動畫處理-單元過快出隊。 我沒有使用這種方法,而是使用了以下方法:
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02f target:self selector:@selector(scroll) userInfo:nil repeats:YES];
- (void)scroll {
tmp += 1;
_collectionView.contentOffset = CGPointMake(tmp, 0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.