0

当我在顶部插入一些项目时,我有一个自定义 UICollectionViewFlowLayout 我计算适当的并将其从targetContentOffsetForProposedContentOffset:.

问题是在顶部的第一个插入批量更新中,UIScrollView调用 in 方法从完全关闭_smoothScrollDisplayLink:的值返回的值覆盖 contentOffset (例如,我返回 900 秒,它会将其覆盖为 500 秒)targetContentOffsetForProposedContentOffset:

在下面的插入批量更新UISCrollView方法中设置的值是非常合理的。

更多信息#1:

我尝试在顶部插入项目,但保持滚动位置不变,所以基本上我将内容偏移设置为当前 contentOffset + 插入更新前后之间的高度增量

更多信息 #2:这是我记录的一些真实值:

current offset=95.500000, newHeight=1835.007812, oldHeight=936.003906
new offset = 994.503906
set content offset: 550.000000 before: 994.503906  (by _smoothScrollDisplayLink:)

current offset=95.500000, newHeight=2771.011719, oldHeight=1835.007812
new offset = 1031.503906
set content offset: 1026.500000 before: 1031.503906 (by _smoothScrollDisplayLink:)
4

1 回答 1

1

我有同样的问题。试试这个技巧:

  1. 在您flowLayout保存的最后一个目标中contentOffset

    - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
    {
        CGPoint contentOffset = [super targetContentOffsetForProposedContentOffset:proposedContentOffset];
    
        _lastTargetContentOffset = contentOffset;
    
        return contentOffset;
    }
    
  2. 在您的collectionView覆盖方法中setContentOffset,如下所示:

    - (void)setContentOffset:(CGPoint)contentOffset
    {
        if (self.contentSize.height != self.collectionViewLayout.collectionViewContentSize.height)
        {
            MyCollectionViewFlowLayout * myCollectionLayout =  (MyCollectionViewFlowLayout *)self.collectionViewLayout;
            NSParameterAssert([myCollectionLayout isKindOfClass:MyCollectionViewFlowLayout.class]);
            [super setContentOffset:myCollectionLayout.lastTargetContentOffset];
        }
        else
        {
            [super setContentOffset:contentOffset];
        }
    }
    
于 2018-03-20T13:06:56.587 回答