尽管滚动,UICollectionView indexPathForItemAtPoint 返回相同的项目 [英] UICollectionView indexPathForItemAtPoint returning same item despite scrolling
问题描述
使用 UICollectionView 的 indexPathForItemAtPoint 的正确语法是什么?
What is the correct syntax for using UICollectionView's indexPathForItemAtPoint?
我正在使用以下代码尝试在滚动时识别位于 UICollectionView 中心的单元格.
I'm using the following code to try to identify the cell that is at the centre of the UICollectionView as I scroll through it.
- (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];
}
然而,无论我如何滚动,这总是给我相同的 indexPath.
However, this always gives me the same indexPath back, no matter how I scroll.
我做错了什么?
推荐答案
您正在单元格的静态字段中查找静态点.是的,您将始终获得相同的单元格.
You are looking up a static point in a static field of cells. Yes you will always get the same cell.
为了处理滚动,您需要将滚动视图的位置添加到您的检查中.像这样:
In order to deal with scrolling you need to add the scrollview's position into your check. Something like this:
- (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];
}
这篇关于尽管滚动,UICollectionView indexPathForItemAtPoint 返回相同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!