1. pagingEnabled = true 可根据页面宽度进行分页
2. 自定义分页功能
pagingEnabled一定要设为false
在scrollViewWillEndDragging:withVelocity:targetContentOffset:里自定义滑动方法
targetContentOffset 是个指针,可以修改这个参数的值,让scrollView最终停止在目标位置。
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
float pageWidth = self.collectionView.width - 28*2 + 10; // width + space
float currentOffset = scrollView.contentOffset.x;
float targetOffset = targetContentOffset->x;
float newTargetOffset = 0;
if (targetOffset > currentOffset)
newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth;
else
newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth;
if (newTargetOffset < 0)
newTargetOffset = 0;
else if (newTargetOffset > scrollView.contentSize.width)
newTargetOffset = scrollView.contentSize.width;
targetContentOffset->x = currentOffset;
[scrollView setContentOffset:CGPointMake(newTargetOffset, 0) animated:YES];
NSLog(F(@"%@",@(newTargetOffset)));
}
若为collectionView也可在UICollectionViewFlowLayout子类里重写
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;
网友评论