0

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

Nitya
  • 829
  • 1
  • 11
  • 25

2 Answers2

1

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);

}
Community
  • 1
  • 1
Nitya
  • 829
  • 1
  • 11
  • 25
0

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

Sviatoslav Yakymiv
  • 7,667
  • 2
  • 18
  • 42