Skip to content

Instantly share code, notes, and snippets.

@juwencheng
Created July 13, 2017 07:16
Show Gist options
  • Save juwencheng/05759e237b58008384c61adc3e9a8562 to your computer and use it in GitHub Desktop.
Save juwencheng/05759e237b58008384c61adc3e9a8562 to your computer and use it in GitHub Desktop.
UICollectionView 分页滚动
// original : https://stackoverflow.com/questions/24303883/uicollectionview-with-paging-enable
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGFloat cellWidth = self.cellWidth;
CGFloat cellPadding = 9;
NSInteger page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1;
if (velocity.x > 0) page++;
if (velocity.x < 0) page--;
page = MAX(page,0);
CGFloat newOffset = page * (cellWidth + cellPadding);
targetContentOffset->x = newOffset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment