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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With