I am using UICollectionView in my iOS application. I have it so that each cell is the width of the screen, and I want to have it, so when it scrolls horizontally, it is fixed on the visible cell and moves it to the center of the screen. In the code below, I only work for the first cell. I do not understand how to make this work for any cell that the user sees.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ int index = 0; for (TextCell * t in [_tabBarCollectionView visibleCells]) { if ((t.center.x>0)&&(t.center.x<[[UIScreen mainScreen]bounds].size.width)) { [_tabBarCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; break; } index++; } }
source share