Created
April 23, 2014 15:36
-
-
Save DarthMike/11220285 to your computer and use it in GitHub Desktop.
Proper collectionView page scrolling with flicks handled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (CGFloat)pageWidth { | |
return self.itemSize.width + self.minimumLineSpacing; | |
} | |
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { | |
if (self.paginationEnabled) { | |
CGFloat rawPageValue = self.collectionView.contentOffset.x / self.pageWidth; | |
CGFloat currentPage = (velocity.x > 0.0) ? floor(rawPageValue) : ceil(rawPageValue); | |
CGFloat nextPage = (velocity.x > 0.0) ? ceil(rawPageValue) : floor(rawPageValue); | |
BOOL pannedLessThanAPage = fabs(1 + currentPage - rawPageValue) > 0.5; | |
BOOL flicked = fabs(velocity.x) > [self flickVelocity]; | |
if (pannedLessThanAPage && flicked) { | |
proposedContentOffset.x = nextPage * self.pageWidth; | |
} else { | |
proposedContentOffset.x = round(rawPageValue) * self.pageWidth; | |
} | |
return proposedContentOffset; | |
} | |
return proposedContentOffset; | |
} | |
- (CGFloat)flickVelocity { | |
return 0.3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment