【问题标题】:ios - UICollectionView change default offset when horizontal pagingios - UICollectionView 在水平分页时更改默认偏移量
【发布时间】:2013-02-14 03:46:13
【问题描述】:

我正在尝试使用 UICollectionView 在屏幕居中的当前视图的左侧和右侧显示视图。

我可以正常显示,但是水平分页不在后续视图的中心,因为它默认为框架的宽度,即 320.0。

UICollectionView 在哪里计算剪切到下一页时使用的默认偏移值?

我想更改此值。有一个更好的方法吗?本质上,我正在尝试重新创建 iPhone 上应用商店搜索结果中使用的体验。

【问题讨论】:

  • 感谢您对真正有帮助的应用商店搜索结果(iOS 6 后)的暗示... ScrollView 的宽度?
  • 不管那些东西(我在上面的评论中提到),你正在寻找的解决方案在这里:blog.proculo.de/archives/…
  • 你是怎么做到的。你能把代码分享给我吗?

标签: iphone ios ios6 uicollectionview


【解决方案1】:

事实证明,我必须设置 pagingEnabled = NO 并使用以下内容覆盖 (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
  if (self.lastQuestionOffset > scrollView.contentOffset.x)
    self.currentPage = MAX(self.currentPage - 1, 0);
  else if (self.lastQuestionOffset < scrollView.contentOffset.x)
    self.currentPage = MIN(self.currentPage + 1, 2);

  float questionOffset = 290.0 * self.currentPage;
  self.lastQuestionOffset = questionOffset;
  [self.collectionView setContentOffset:CGPointMake(questionOffset, 0) animated:YES];
}

这个答案有帮助: Paging UIScrollView with different page widths

【讨论】:

  • 我发现的另一件事是在滚动时使用它:[self.collectionView scrollRectToVisible:CGRectMake(questionOffset, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height) animated:YES]; 它比上面使用的 setContentOffset 动画方法更流畅。
  • 我只是在尝试使 UICollection 视图的行为类似于 iPhone iOS AppStore 的特色页面顶部的较大矩形时遇到了这个问题。我目前一直在 - (CGPoint)targetContentOffsetForProposedContentOffset:withScrollingVelocity 工作。但我正在考虑实施像您这样的解决方案。不过我有一个问题,您的 self.lastQuestionOffset 属性是否类似于(单元格大小 + 插图)*(单元格数)?
  • 一个更好的解决方案是使用 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 然后根据您的分页意愿计算和设置目标内容(这是一个 inout 参数)
  • @GreatWiz 会起作用,但它不会“感觉”像分页。见方法二here下的讨论。
  • @JKLaiho 效果很好。我用它实现了一个类似胶片的界面,在 Mela App 中双向分页。你可以试试itunes.apple.com/us/app/mela/id868065558?mt=8
【解决方案2】:

我认为最好的解决方案是添加自定义 UICollectionViewFlowLayout 子类并覆盖

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset  
                                 withScrollingVelocity:(CGPoint)velocity

我在这里写了一个例子:https://gist.github.com/mmick66/9812223

【讨论】:

    【解决方案3】:

    尝试在 viewDidLayoutSubviews

    中设置 contentOffset
    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
        if (viewDidLayoutSubviewsForTheFirstTime) {
            _collectionView.contentOffset = CGPointMake(...);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2016-10-04
      • 2019-11-07
      • 2016-08-07
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多