菜鸟教程小白 发表于 2022-12-13 11:46:06

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


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

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

<pre><code>- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout 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 = ;
    CGFloat width = la.size.width;

    // this must be wrong, too.what about insets, header views, etc?
    NSInteger firstVisible = floorf(targetContentOffset-&gt;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&lt;lastVisible; i++) {
      ];
    }
}
</code></pre>

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

<p>请告诉我,我在 sdk 中已经遗漏了一些简单的东西。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为使用 <code>UICollectionView indexPathForItemAtPoint:</code> 方法会更好。</p>

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

<p>然后使用这两个点得到两个对应的 <code>indexPath</code> 值。这将为您提供 <code>firstVisible</code> 和 <code>lastVisible</code> 索引路径。</p>

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

    NSIndexPath *firstVisible = ;
    firstVisible = (firstVisible)? firstVisible : ;
    NSIndexPath *lastVisible = ;
    lastVisible = (lastVisible)? lastVisible : ;

    NSMutableArray *willBeVisibleIndexPaths = [@[] mutableCopy];
    for (NSInteger i=firstVisible.row; i&lt;lastVisible.row; i++) {
      ];
    }
}
</code></pre>

<p>这只是部分解决方案。很可能存在 <code>lastVisible</code> 为 <code>nil</code> 的情况。您需要检查它并将 <code>lastVisible</code> 设置为集合的最后一个 <code>indexPath</code> 应该是什么。由于这些点位于页眉或页脚 View 中,<code>firstVisible</code> 或 <code>lastVisible</code> 也可能为 <code>nil</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios -ScrollViewdidEndDragging 时预测可见索引路径,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33636874/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33636874/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - ScrollView didEndDragging 时预测可见索引路径