简体   繁体   中英

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.

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.

Where is UICollectionView calculating the default offset value it uses when clipping to the next pages?

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.

It turned out that I had to set pagingEnabled = NO and overrode (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView with the following:

- (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

I think that the best solution is to add a custom UICollectionViewFlowLayout subclass and override the

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

I wrote an example here: https://gist.github.com/mmick66/9812223

try to set contentOffset in viewDidLayoutSubviews

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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