You do not have to implement scrollViewWillEndDragging:withVelocity:targetContentOffset:
method to enable paging
in a UIScrollView
. To do this, you need only:
- Set
pagingEnabled
property of your scrollview toYES
. Documentation says about this property: If the value of this property is YES, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. - Add your subviews (pages) to the scrollview. Their
width
s must be equal to the scrollview's width, to retrieve a horizontal-paged scrollview. - Set your scrollview's
contentSize
property.
So your code should be something like this:
[scrollView setPagingEnabled:YES];
NSInteger numberOfPages = 100;
CGFloat pageWidth = scrollView.bounds.size.width;
CGFloat pageHeight = scrollView.bounds.size.height; // can be something else
for(NSInteger i = 0; i < numberOfPages; i++)
{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(i * pageWidth, 0, pageWidth, pageHeight)];
// do somethings with the page view
[scrollView addSubview:view];
}
[scrollView setContentSize:CGSizeMake(numberOfPages * pageWidth, scrollView.bounds.size.height)];