how to get indexPath for cell which is located in the center of UICollectionView

前端 未结 11 2244
慢半拍i
慢半拍i 2020-12-25 09:41

How can i find indexPath for cell in the middle of UICollectionView?

I have horizontal scrolling and only one big cell<

11条回答
  •  温柔的废话
    2020-12-25 10:33

    Thank you micantox!

    I had multiple visible cells of UICollectionView and needed to position the cell at the centre of the collection view. Something similar to Adobe Content Viewer. If someone is struggling with similar scenario:

    #pragma mark - UIScrollViewDelegate methods
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        CGPoint centerPoint = CGPointMake(self.pageContentCollectionView.center.x + self.pageContentCollectionView.contentOffset.x,
                                        self.pageContentCollectionView.center.y + self.pageContentCollectionView.contentOffset.y);
        NSIndexPath *centerCellIndexPath = [self.pageContentCollectionView indexPathForItemAtPoint:centerPoint];
        [self.pageContentCollectionView scrollToItemAtIndexPath:centerCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    }
    

提交回复
热议问题