Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 718
whittwuli Avatar asked Feb 14 '13 03:02

whittwuli


2 Answers

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

like image 150
whittwuli Avatar answered Oct 13 '22 01:10

whittwuli


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

like image 31
Mike M Avatar answered Oct 13 '22 00:10

Mike M