Ios 滚动视图拖动时预测可见索引路径

Ios 滚动视图拖动时预测可见索引路径,ios,uiscrollview,uicollectionview,Ios,Uiscrollview,Uicollectionview,我有一个具有水平流动布局和固定宽度单元格的集合视图。当用户结束拖动时,我希望在为减速完成后可见的项目获取内容方面取得领先 为此,我需要在减速结束时可见的索引路径。我认为这段代码是可行的,但很蹩脚(出于显而易见的原因,我认为,只是注释中描述的其中一些代码): -(void)ScrollViewWillendDraging:(UIScrollView*)带速度的scrollView:(CGPoint)速度targetContentOffset:(inout CGPoint*)targetConten

我有一个具有水平流动布局和固定宽度单元格的集合视图。当用户结束拖动时,我希望在为减速完成后可见的项目获取内容方面取得领先

为此,我需要在减速结束时可见的索引路径。我认为这段代码是可行的,但很蹩脚(出于显而易见的原因,我认为,只是注释中描述的其中一些代码):

-(void)ScrollViewWillendDraging:(UIScrollView*)带速度的scrollView:(CGPoint)速度targetContentOffset:(inout CGPoint*)targetContentOffset{
//已经在这里游荡了:
//a)这似乎是获得固定单元格宽度的错误方法
//b)遗憾的是,该方法排除了可变宽度单元格
UICollectionViewLayoutAttributes*la=[self.collectionView.CollectionViewLayoutLayoutAttributesForElementsRect:self.collectionView.bounds][0];
CGFloat width=la.size.width;
//这肯定也是错误的。那么插图、标题视图等呢?
NSInteger firstVisible=地板(targetContentOffset->x/宽度);
NSInteger visibleCount=ceilf(self.collectionView.bounds.size.width/width);
NSInteger lastVisible=MIN(firstVisible+visibleCount,self.model.count);
NSMutableArray*将可见IndExpaths=[@[]mutableCopy];
//忽略部分

对于(NSInteger i=firstVisible;i我认为最好使用
UICollectionView indexPathForItemAtPoint:
方法

根据
targetContentOffset
和集合视图的
contentSize
计算集合视图可见区域的左上角和右下角点

然后使用这两点获得两个对应的
indepath
值。这将为您提供
firstVisible
lastVisible
索引路径

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    UICollectionView *collectionView = (UICollectionView *)scrollView;
    CGPoint topLeft = CGPointMake(targetContentOffset->x + 1, targetContentOffset->y + 1);
    CGPoint bottomRight = CGPointMake(topLeft.x + scrollView.bounds.size.width - 2, topLeft.y + scrollView.bounds.size.height - 2);

    NSIndexPath *firstVisible = [collectionView indexPathForItemAtPoint:topLeft];
    firstVisible = (firstVisible)? firstVisible : [NSIndexPath indexPathForItem:0 inSection:0];
    NSIndexPath *lastVisible = [collectionView indexPathForItemAtPoint:bottomRight];
    lastVisible = (lastVisible)? lastVisible : [NSIndexPath indexPathForItem:self.model.count-1 inSection:0];

    NSMutableArray *willBeVisibleIndexPaths = [@[] mutableCopy];
    for (NSInteger i=firstVisible.row; i<lastVisible.row; i++) {
        [willBeVisibleIndexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
    }
}
-(void)ScrollViewWillendDraging:(UIScrollView*)带速度的scrollView:(CGPoint)速度targetContentOffset:(inout CGPoint*)targetContentOffset{
UICollectionView*collectionView=(UICollectionView*)滚动视图;
CGPoint topLeft=CGPointMake(targetContentOffset->x+1,targetContentOffset->y+1);
CGPoint bottomRight=CGPointMake(topLeft.x+scrollView.bounds.size.width-2,topLeft.y+scrollView.bounds.size.height-2);
NSIndexPath*firstVisible=[collectionView indexPathForItemAtPoint:topLeft];
firstVisible=(firstVisible)?firstVisible:[NSIndexPath indexPathForItem:0第0节];
NSIndexPath*lastVisible=[collectionView indexPathForItemAtPoint:bottomRight];
lastVisible=(lastVisible)?lastVisible:[NSIndexPath indexPathForItem:self.model.count-1安全组:0];
NSMutableArray*将可见IndExpaths=[@[]mutableCopy];

for(NSInteger i=firstVisible.row;iThanks very.Will)将根据警告制定更通用的版本(并将修复帖子中的一些编译错误)然后建议在这里进行编辑。你能解释一下左上角点的+1增量和右下角点的-1减量吗?请随意编辑我的答案以修正任何打字错误。我只是在没有任何测试的情况下输入了答案。+1/-1只是为了“插入”这些点有助于确保这些点实际上位于这些点的项目内。如果您不同意其中任何一点,请告诉我。经过一些测试,代码的主要问题(在编辑中修复)依赖于
contentSize
而不是
bounds.size
来形成右下角点。我仍然不确定的另一个问题是如何设置左上角点和右下角点的微小移动。在我的情况下,我需要在单元格内添加和减去多个1。我认为这与单元格之间的差异有关高度和集合视图高度,但我不知道如何可靠地计算。
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    UICollectionView *collectionView = (UICollectionView *)scrollView;
    CGPoint topLeft = CGPointMake(targetContentOffset->x + 1, targetContentOffset->y + 1);
    CGPoint bottomRight = CGPointMake(topLeft.x + scrollView.bounds.size.width - 2, topLeft.y + scrollView.bounds.size.height - 2);

    NSIndexPath *firstVisible = [collectionView indexPathForItemAtPoint:topLeft];
    firstVisible = (firstVisible)? firstVisible : [NSIndexPath indexPathForItem:0 inSection:0];
    NSIndexPath *lastVisible = [collectionView indexPathForItemAtPoint:bottomRight];
    lastVisible = (lastVisible)? lastVisible : [NSIndexPath indexPathForItem:self.model.count-1 inSection:0];

    NSMutableArray *willBeVisibleIndexPaths = [@[] mutableCopy];
    for (NSInteger i=firstVisible.row; i<lastVisible.row; i++) {
        [willBeVisibleIndexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
    }
}