How to create a horizontal scrolling view for the iPhone?
iphone, scroll
Solution
You need:
- A ScrollView with paging enabled, three screens wide.
- To have three subviews in your scrollview - L (left content, at 0,0), C (centre content, at 320,0), R (right content, at 640,0). C is the content you want to initially show. L is the content to show if the user scrolls left.
- Set the contentoffset of the scrollview to 320 (for portrait mode), because you want C to display initially, not L.
- Every time a scroll finishes, the contentoffset will be an integer multiple of screens. If you are still displaying C (contentoffset is 320), then you're fine. If the contentoffset is now 0 or 640, you have a bit of work to do.
If the user scrolls left - you have the same view hierarchy, but now the scrollview is showing L rather than C, because the contentoffset is 0, with a scrolloffset of 0. You should now reset all the content so you have XLC and a scrolloffset of 320 - in case the user wants to scroll left again. In other words, C becomes the new R, L becomes the new C, and X is the new content to display if the user scrolls left again.
The method to override to do this is:
(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
Problem
Safari does this with tabbed web pages. The AppStore (3.0) does this with an app's preview images. Views scroll horizontally and lock in centered on each view. Any idea how to accomplish this?