【问题标题】:UICollectionView targetContentOffsetForProposedContentOffset: Overridden by UIScrollViewUICollectionView targetContentOffsetForProposedContentOffset:被 UIScrollView 覆盖
【发布时间】:2017-12-28 14:18:37
【问题描述】:

当我在顶部插入一些项目时,我有一个自定义 UICollectionViewFlowLayout,我计算出合适的并从targetContentOffsetForProposedContentOffset: 返回。

问题是在顶部的第一个插入批处理更新 UIScrollView 方法中称为 _smoothScrollDisplayLink:override 从 targetContentOffsetForProposedContentOffset: 返回的值的 contentOffset 完全关闭的值(我返回例​​如 900s 它覆盖到 500 秒)

但是

在下面的插入批量更新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:)

【问题讨论】:

    标签: objective-c uiscrollview uicollectionview uicollectionviewlayout uicollectionviewflowlayout


    【解决方案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];
          }
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多