尽管滚动,UICollectionView indexPathForItemAtPoint返回相同的项目

the*_*ncs 8 objective-c ios uicollectionview

使用UICollectionView的indexPathForItemAtPoint的正确语法是什么?

当我滚动它时,我正在使用以下代码来尝试识别位于UICollectionView中心的单元格.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    //locate the scrollview which is in the centre
    CGPoint centerPoint = CGPointMake(self.collectionView.frame.size.width / 2, self.collectionView.frame.size.height /2);
    NSIndexPath *indexPathOfCentralCell = [self.collectionView indexPathForItemAtPoint:centerPoint];
}
Run Code Online (Sandbox Code Playgroud)

但是,无论我如何滚动,这总是给我相同的indexPath.

我究竟做错了什么?

Put*_*103 33

您正在静态单元格中查找静态点.是的,你将永远得到相同的细胞.

为了处理滚动,您需要将scrollview的位置添加到支票中.像这样的东西:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    //locate the scrollview which is in the centre
    CGPoint centerPoint = CGPointMake(self.collectionView.frame.size.width / 2 + scrollView.contentOffset.x, self.collectionView.frame.size.height /2 + scrollView.contentOffset.y);
    NSIndexPath *indexPathOfCentralCell = [self.collectionView indexPathForItemAtPoint:centerPoint];
}
Run Code Online (Sandbox Code Playgroud)