![](/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.