I have a vertical UICollectionView
with each cell taking up the entire self.view.frame
I can easily swipe upwards to page to the next cell, but I would like to do the same thing with the press of a button.
I've tried using:
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
And:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
They work but they temporarily "White-Out" the currentCell to present the nextCell, while the swiping shows both cells during the transition.
Ideally I would like to use:
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
But I don't know how to access the nextCell's indexPath
...
I've tried:
NSIndexPath *nextIndexPath = [_collectionView indexPathForItemAtPoint:CGPointMake(25, (_collectionView.frame.size.height-25)*(_currentIndexRowForCellOnScreen+1))];
Where _currentIndexRowForCellOnScreen
is the indexPath.row
of the UICollectionView
's first appearance on screen in:
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
But when I put it into:
- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
it returns NULL after the first Cell, and does not animate....
Any direction would be greatly appreciated. Thank you for your time.
Assuming your collectionView contains only 1 section, and given that each item occupies the whole frame then you could do something like this;
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
I'm ashamed I did not know about the NSIndexPath properties .item and .section. It doesn't appear in the NSIndexPath Class Reference unless you search it within the UIKit Framework Reference. But for some reason I am still getting the same results of the "white-Out" during the transition. Thank you for your answer, it did answer my initial question, so I will give you credit.
you shouldn't be getting white-out ... i tested it and i saw, as expected, scrolling to the correct position. With that said, i used an existing collectionView with multiple cells visible. I would look at your cell, ensure that you are rasterizing the layer or you might see, in the least hesitation in scrolling.
please note that indexPathsForVisibleItems may need sorting to have the biggest number at index 0
I know this is an old discussion, but I am getting the "white-out" as well. In my case I have an inset at the top of the UICollectionView to reveal something behind it. When you drag the collectionview up it scrolls in front of it. That all works perfectly and beautifully. HOWEVER, only when I set the scroll position programmatically I do get the whole top whited out, at the exact height of the inset. Then when I drag the collection a tiniest bit by hand, the view behind it is visible again. This problem has been driving me nuts! :-( Anybody else out there who experienced this and solved it?
Further testing reveals that the "white-out" is the background color of the UICollectionView. When I set it to a red background color, it will be red. Also, the height of it is dependent on the scroll position before I programmatically adjust the scroll position (but it maxes out at the top of course). So, it's as if setting the scroll position programmatically adjusts the grid of the collection view, but not the area that should hold the background color. Tricky..!