user542687
user542687

Reputation:

Restricting UIScrollView scrolling to only one page at a time

I would like to limit a UIScrollView to only being able to scroll one page at a time.

In other words, even when the user flicks really fast, I want the UIScrollView to be restricted to scrolling only one page.

Is there a good way to do this?

Upvotes: 3

Views: 2967

Answers (2)

MaxGabriel
MaxGabriel

Reputation: 7707

One way to achieve this, if you haven't already settled on the scroll view, is to use a UIPageViewController. I'm looking at one on my app and this functionality seems to come for free.

With the UIScrollView, I'm thinking you might want to respond to the UIScrollViewDelegate method - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

I was thinking you could hold your current 'page' in a property, and then in the scrollViewWillEndDragging:withVelocity:targetContentOffset: method you could modify the target content offset to only be the next/previous page.

There's a 2012 WWDC video, Enhancing User Experience with Scroll Views, that uses this approach if you wanted to see it in action.

Upvotes: 1

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

UIScrollView *myScrollView;
...

myScrollView.pagingEnabled = YES;

When set to YES, the scroll view stops on multiples of the scroll view’s.

Upvotes: 6

Related Questions