繁体   English   中英

ios - UICollectionView在水平分页时更改默认偏移量

[英]ios - UICollectionView change default offset when horizontal paging

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

我有正确显示但水平分页不以后续视图为中心,因为它默认为帧的宽度,即320.0。

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

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

事实证明,我必须设置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];
}

这个答案有所帮助: 使用不同的页面宽度分页UIScrollView

我认为最好的解决方案是添加一个自定义的UICollectionViewFlowLayout子类并重写

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

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

尝试在viewDidLayoutSubviews中设置contentOffset

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM