Using a really big contentSize
isn't the way to go. contentSize
still uses fixed datatypes, and if you scroll long enough, they'll overflow and at best, your drawing will go haywire. Worst case, your app crashes.
What you want is to give the impression of infinite scrolling by using a window. I'll illustrate how it works with a simple 1-D example, but you can easily extend it to 2-D.
Let's say you have 3 entries, where each one fills the UIScrollView
. Scrolling to the right, it would appear to be arranged like this:
A B C A B C A B C ...
Internally, you're going to arrange them like this:
C A B C
Because when A is visible, you can see part of C if you swipe to the right or part of B if you swipe to the left.
Your UIScrollView
's contentOffset
is your window. While contentSize
encompasses the width of all four entities (C A B C
), internally you're going to restrict that to 75% of the actual width. As your user scrolls left and right, you adjust contentOffset
so that it is never negative or more than 75% of contentSize.width
. This is done inside your delegate, in scrollViewDidScroll:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
while (scrollView.contentOffset.x < 0)
scrollView.contentOffset.x += constrainedContentSize.width;
while (scrollView.contentOffset.x > constrainedContentSize.width)
scrollView.contentOffset.x -= constrainedContentSize.width;
// ...
}
Note that this assumes an instance variable constrainedContentSize
, likely in the controller for the view that your UIScrollView
is inside, and that the controller is your UIScrollView
delegate.
This will be far more efficient than constantly releasing and recreating views.