I am implementing a UICollectionView in my iOS app. I have it so each cell is the width of the screen and I want to have it so when scrolling horizontally it locks onto the visible cell and moves it to the center of the screen. The code bellow I have only works for the first cell. I don't understand how to get this to work for any cell the user has visible.
- (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++;
}
}