Skip to content

Instantly share code, notes, and snippets.

@DarthMike
Created April 23, 2014 15:36
Show Gist options
  • Save DarthMike/11220285 to your computer and use it in GitHub Desktop.
Save DarthMike/11220285 to your computer and use it in GitHub Desktop.
Proper collectionView page scrolling with flicks handled
- (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