7

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.

whittwuli
  • 231
  • 1
  • 3
  • 9
  • Thankyou for your allusion to the appstore search results (post iOS 6) that really helped... I would like to know what you mean when you say that's the frame of the scrollView, do you mean that's the width of your ScrollView? – Albert Renshaw Feb 14 '13 at 04:44
  • Regardless of that stuff (I referred to in the comment above), the solution you are looking for is here: http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html – Albert Renshaw Feb 14 '13 at 04:46
  • How did you do it. Could you please share me the code? – Md Rais Mar 31 '18 at 11:59

3 Answers3

11

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

Community
  • 1
  • 1
whittwuli
  • 231
  • 1
  • 3
  • 9
  • 2
    One more thing I found was to use this when scrolling: `[self.collectionView scrollRectToVisible:CGRectMake(questionOffset, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height) animated:YES];` It works much smoother then the setContentOffset animated method used above. – whittwuli Feb 15 '13 at 17:29
  • I just stubbled across this while trying to make a UICollection view behave like the larger Rectangles at the top of the featured page if the iPhone iOS AppStore. I have currently been working in - (CGPoint)targetContentOffsetForProposedContentOffset:withScrollingVelocity. But I am thinking of implementing a solution like yours instead. I have a question though, Is your self.lastQuestionOffset property something like (size of cell + inset) * (number of cells)? – vichudson1 May 01 '13 at 19:14
  • An even better solution would be to use - `(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset` then calculate and set the target content according to your paging wishes (it's an *inout* parameter) – GreatWiz Feb 08 '14 at 16:14
  • @GreatWiz that will sort of work, but it won't "feel" like paging. See the discussion under method 2 [here](http://tumblr.soncode.com/post/65677240870/tips-for-implementing-custom-width-paging-in-ios). – JK Laiho Jun 09 '14 at 07:38
  • @JKLaiho it works quite well. I implemented a film strip like interface with it, with paging in both directions in Mela App. You can try it out https://itunes.apple.com/us/app/mela/id868065558?mt=8 – GreatWiz Jun 10 '14 at 15:23
  • @whittwuli what is `lastContentOffset` ? – Saheb Singh Aug 06 '14 at 13:13
5

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

Mike M
  • 4,879
  • 5
  • 38
  • 58
0

try to set contentOffset in viewDidLayoutSubviews

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