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++;
}
}
You can turn paging on for the collection view and it will have that effect. Go to the xib file or storyboard that has the collection added and enable paging under its properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With