praveena
praveena

Reputation: 723

How to add auto scroll functionality to a view in iphone sdk?

I'm new to auto scroll functionality. I have a very lengthy .png file. what I want to do is, when I click a button I want to automatically start scrolling of that image. I had gone through scrollView sample code in sdk. but I get confused. Please any one help me

Thanks in advance Praveena

Upvotes: 0

Views: 2813

Answers (2)

user1573321
user1573321

Reputation:

- (void) scrollView
{
    CGFloat currentOffset = scrollImage.contentOffset.x;

    CGFloat newOffset;

    if(currentOffset == 3840)
    {
        currentOffset = -320.0;

        [scrollImage setContentOffset:CGPointMake(newOffset,0.0) animated:NO];
    }

    newOffset = currentOffset + 320;

        [UIScrollView beginAnimations:nil context:NULL]; 
        [UIScrollView setAnimationDuration:2.1]; 
        [scrollImage setContentOffset:CGPointMake(newOffset,0.0)];
        [UIScrollView commitAnimations];
}

Upvotes: 4

Morten Fast
Morten Fast

Reputation: 6320

Scrolling is achieved by setting the content offset.

Imagine a view controller constructed something like this (e.g. in -viewDidLoad):

// Load image.
UIImage *image = [UIImage imageNamed:@"image.png"];

// Create image view to hold the image.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];

[imageView setImage:image];

// Create a scroll view that is the size of the view.
scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];

// Important: If the content size is less than the scroll view frame, then it will not scroll.
[scrollView setContentSize:[image size]];

// Add to view hierarchy.
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];

To make it scroll, just do this:

[scrollView setContentOffset:CGPointMake(0, 100) animated:YES];

To have the scrolling to be continuous, you need to set up a timer that updates the content offset.

Upvotes: 0

Related Questions