views:

541

answers:

2

Hi,

i want to implement a ipad view, where i parse an xml file and put the entries side by side on an "endless" ipad view, so you have to swipe through it left and right. could someone tell me how i can implement this? which type of view do i have to use?

thanks in advance

regards

+1  A: 

You use UIScrollView with paging enabled. The essentials will be setting myscrollView.contentSize to the total width of all your pages, but creating the individual pages on a just-in-time basis as the scrolling is happening (see the UIScrollViewDelegate documentation); in other words you don't want to actually have dozens or more views using up memory when all you need is three -- the current view and plus the previous and next views. This sample code from apple should get you started: http://developer.apple.com/iphone/library/samplecode/Scrolling/Introduction/Intro.html

wkw
A: 

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.

Steve Madsen