简体   繁体   English

隐藏一半时,UICollectionView scrollToItemAtIndexPath单元消失

[英]UICollectionView scrollToItemAtIndexPath cell disappears when half hidden

I'm trying to automatically move cells in my UICollectionView . 我正在尝试自动移动UICollectionView单元格。 The code to move it: 移动它的代码:

- (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) {
    }];
}

My cells are organized horizontally in 2 rows pattern: 我的单元格以2行模式水平组织:

|cell||cell||cell|->etc
|cell||cell||cell|->etc

The problem is that when scroll moves to the next cell, so whole collectionView moves, previous cell disappears. 问题在于,当滚动移动到下一个单元格时,整个collectionView都会移动,上一个单元格会消失。 It happens exactly when half of the previous cell is not visible on the screen. 恰好在屏幕上看不到上一个单元格的一半时发生。 Why can it happen? 为什么会发生? Any idea how to fix the issue? 任何想法如何解决该问题?

It looks like: (~ means screen) 看起来像:(〜表示屏幕)

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

What is more, the delegate: collectionView:didEndDisplayingCell:forItemAtIndexPath: gets called when the cell is half visible (so exactly between step 3&4) 更重要的是,在单元格只有一半可见时(因此恰好在步骤3和4之间),将调用委托: collectionView:didEndDisplayingCell:forItemAtIndexPath:

Ok, so it went out that one can't make animation on this - the cells were dequeued too fast. 好的,结果是无法对此进行动画处理-单元过快出队。 Instead of this approach I've used something like this: 我没有使用这种方法,而是使用了以下方法:

_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM