Ios - UICollectionView changes default offset when horizontal swap

I am trying to use a UICollectionView to display the view to the left and right of the current view, which is focused on the screen.

This is displayed correctly for me, but horizontal paging is not focused on subsequent views, since by default it is equal to the width of the frame, which is equal to 320.0.

Where does the UICollectionView calculate the default offset value that it uses when clipping to the following pages?

I would like to change this value. Is there a better way to do this? In fact, I'm trying to recreate the experience used in search results for the app store on the iphone.

+6
source share
3 answers

It turned out that I had to set pagingEnabled = NO and override (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView as follows:

 - (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 a UIScrollView with different page widths

+11
source

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

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

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

+6
source

try setting contentOffset to viewDidLayoutSubviews

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

All Articles