菜鸟教程小白 发表于 2022-12-12 19:03:54

ios - 尝试使用自定义布局使屏幕外 UICollectionViewCells 预渲染


                                            <p><p>我有一个分页的 UICollectionView,每页有一个单元格。但是,我的 UICollectionViewCells 渲染速度很慢(第三方库,没有选项)。所以作为优化,我需要预先渲染当前页面左右两边的单元格,这样滚动很流畅。</p>

<p>我所做的是,在我的自定义 UICollectionViewLayout 中,在检查应该返回哪些单元格的 UICollectionViewLayoutAttributes 之前,将传递的 rect 偏移 1 点,请参见此处:</p>

<pre><code>- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    CGRect outsetRect = CGRectInset(rect, -1, 0);
    return [_myAttributes filter:^BOOL(UICollectionViewLayoutAttributes *attribs) {
      return CGRectIntersectsRect(attribs.frame, outsetRect);
    }];
}
</code></pre>

<p>这按预期工作。然而,当实际的 UICollectionView 做它的事情时,它似乎忽略了屏幕外的单元格,并且只为第一个单元格调用 <code>cellForItemAtIndexPath</code>,而不是右侧(或左侧)的单元格。</p>

<p>有什么想法吗?谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为了将来引用,我通过以下方式解决了这个问题:</p>

<ul>
<li>使用于预渲染的单元格在屏幕上强制移动一点,但大小相同。</li>
<li>将其 z-index 更改为 -1,使其位于主页单元格下方。</li>
<li>在 layoutAttributesForElementsInRect 中,检查矩形是否为两页宽,如果是则在页面之间滚动,所以不要抓取任何多余的页面</li>
<li>在我的单元格的 applyLayoutAttributes 中,我检查 z-index 是否<0,如果是,那么这是一个预渲染单元格。</li>
</ul>

<p>干杯</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 尝试使用自定义布局使屏幕外 UICollectionViewCells 预渲染,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/21899945/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/21899945/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 尝试使用自定义布局使屏幕外 UICollectionViewCells 预渲染