简体   繁体   English

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

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

I'm trying to use UICollectionView to display the view to the left and right of the current view that is centered in the screen. 我正在尝试使用UICollectionView来显示屏幕中心的当前视图左侧和右侧的视图。

I have this displaying properly but the horizontal paging does not center on the subsequent views because it defaults to the width of the frame, which is 320.0. 我有正确显示但水平分页不以后续视图为中心,因为它默认为帧的宽度,即320.0。

Where is UICollectionView calculating the default offset value it uses when clipping to the next pages? UICollectionView在哪里计算剪切到下一页时使用的默认偏移值?

I would like to change this value. 我想改变这个值。 Is there a better way to do this? 有一个更好的方法吗? Essentially I am trying to recreate the experience being used in the search results for the app store on the iphone. 基本上我正在尝试重新创建在iphone上的应用商店的搜索结果中使用的体验。

It turned out that I had to set pagingEnabled = NO and overrode (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView with the following: 事实证明,我必须设置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];
}

This answer helped: Paging UIScrollView with different page widths 这个答案有所帮助: 使用不同的页面宽度分页UIScrollView

I think that the best solution is to add a custom UICollectionViewFlowLayout subclass and override the 我认为最好的解决方案是添加一个自定义的UICollectionViewFlowLayout子类并重写

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

I wrote an example here: https://gist.github.com/mmick66/9812223 我在这里写了一个例子: https//gist.github.com/mmick66/9812223

try to set contentOffset in viewDidLayoutSubviews 尝试在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