简体   繁体   中英

UICollectionView scrollToItemAtIndexPath cell disappears when half hidden

I'm trying to automatically move cells in my 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:

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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