• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - ScrollView didEndDragging 时预测可见索引路径

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:46:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有水平流布局和固定宽度单元格的 Collection View 。当用户结束拖动时,我想抢先获取在减速完成时将可见的项目的内容。

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

- (void)scrollViewWillEndDraggingUIScrollView *)scrollView withVelocityCGPoint)velocity targetContentOffsetinout CGPoint *)targetContentOffset {
    // already bummed here: 
    // a) this seems the wrong way to get the fixed cell width
    // b) sad that this method precludes variable width cells
    UICollectionViewLayoutAttributes *la = [self.collectionView.collectionViewLayout layoutAttributesForElementsInRect:self.collectionView.bounds][0];
    CGFloat width = la.size.width;

    // this must be wrong, too.  what about insets, header views, etc?
    NSInteger firstVisible = floorf(targetContentOffset->x / width);

    NSInteger visibleCount = ceilf(self.collectionView.bounds.size.width / width);
    NSInteger lastVisible = MIN(firstVisible+visibleCount, self.model.count);
    NSMutableArray *willBeVisibleIndexPaths = [@[] mutableCopy];

    // neglecting sections
    for (NSInteger i=firstVisible; i<lastVisible; i++) {
        [willBeVisibleIndexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
    }
}

这是很多脆弱的代码来做一些看起来直截了当的事情。如果我想让它处理部分、插图、辅助 View 、可变单元格等。它很快就会变成一个错误的、低效的缠结。

请告诉我,我在 sdk 中已经遗漏了一些简单的东西。



Best Answer-推荐答案


我认为使用 UICollectionView indexPathForItemAtPoint: 方法会更好。

根据targetContentOffset和collection view的contentSize计算collection view可见区域的左上角和右下角点。

然后使用这两个点得到两个对应的 indexPath 值。这将为您提供 firstVisiblelastVisible 索引路径。

- (void)scrollViewWillEndDraggingUIScrollView *)scrollView withVelocityCGPoint)velocity targetContentOffsetinout 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]];
    }
}

这只是部分解决方案。很可能存在 lastVisiblenil 的情况。您需要检查它并将 lastVisible 设置为集合的最后一个 indexPath 应该是什么。由于这些点位于页眉或页脚 View 中,firstVisiblelastVisible 也可能为 nil

关于ios - ScrollView didEndDragging 时预测可见索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636874/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap