Objective c UICollectionView targetContentOffsetForProposedContentOffset:由UIScrollView覆盖

Objective c UICollectionView targetContentOffsetForProposedContentOffset:由UIScrollView覆盖,objective-c,uiscrollview,uicollectionview,uicollectionviewlayout,uicollectionviewflowlayout,Objective C,Uiscrollview,Uicollectionview,Uicollectionviewlayout,Uicollectionviewflowlayout,我有一个自定义的UICollectionViewFlowLayout,当我在顶部插入一些项目时,我计算相应的值并从targetContentOffsetForProposedContentOffset:返回它 问题在于第一次插入时,在名为\u smoothScrollDisplayLink:的方法中,批更新顶部的UIScrollView,将从targetContentOffsetForProposedContentOffset:返回的值中的contentOffset覆盖为完全关闭的值(例如,我返

我有一个自定义的UICollectionViewFlowLayout,当我在顶部插入一些项目时,我计算相应的值并从
targetContentOffsetForProposedContentOffset:
返回它

问题在于第一次插入时,在名为
\u smoothScrollDisplayLink:
的方法中,批更新顶部的
UIScrollView
,将从
targetContentOffsetForProposedContentOffset:
返回的值中的contentOffset覆盖为完全关闭的值(例如,我返回900s,它将覆盖为500s)

但是

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

更多信息#1:

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

更多信息#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:)

我也有同样的问题。试试这个黑客:

  • flowLayout
    保存最后一个目标
    contentOffset

    - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
    {
        CGPoint contentOffset = [super targetContentOffsetForProposedContentOffset:proposedContentOffset];
    
        _lastTargetContentOffset = contentOffset;
    
        return contentOffset;
    }
    
  • 在您的
    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];
        }
    }