Nitya
Nitya

Reputation: 859

Get Item (or index) of UICollectionView when it stops scrolling

Is there any easy way of getting Item or index of item when UICollectionView stops scrolling?

Upvotes: 0

Views: 703

Answers (2)

Nitya
Nitya

Reputation: 859

This is how I found what item is available on center of the page when UICollectionView Stop Scrolling. Based on this Answer. https://stackoverflow.com/a/24396643/2618600

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"Finished scrolling %@",scrollView);

    if ([scrollView isKindOfClass:[UICollectionView class]])
    {

        UICollectionView *mainCollection = (UICollectionView *) scrollView;


  CGRect visibleRect = (CGRect){.origin = mainCollection.contentOffset, .size = mainCollection.bounds.size};
        CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
        NSIndexPath *visibleIndexPath = [mainCollection indexPathForItemAtPoint:visiblePoint];
        NSLog(@"visibleIndexPath %ld",(long)visibleIndexPath.row);

}

Upvotes: 1

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

You can use - (NSArray *)indexPathsForVisibleItems method of UICollectionView.

Upvotes: 0

Related Questions