Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Scrolling Horizontally

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

}
like image 437
BDGapps Avatar asked Oct 05 '22 06:10

BDGapps


1 Answers

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.

like image 149
Kris Gellci Avatar answered Oct 13 '22 12:10

Kris Gellci