Sheehan Alam
Sheehan Alam

Reputation: 60919

How to programmatically scroll a UIScrollView?

I have a UIScrollView that I can swipe through to display new views. I have a button that I want to programmatically scroll through the views. So if I click it once, it will scroll to the next view. How can I implement this?

Here is the scrollview code

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{
    NSUInteger offset = aScrollView.contentOffset.x;
    NSUInteger width = aScrollView.contentSize.width;
    int anIndex = (offset == 0) ? 0 : (int)(width/(width - offset));
    selectedDeal = [deals objectAtIndex:(anIndex > [deals count]- 1 ? [deals count]- 1 : anIndex)];
}

Upvotes: 3

Views: 3895

Answers (2)

Mani
Mani

Reputation: 1841

[myScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0)animated:YES];

[myScrollView scrollRectToVisible:self.view.frame animated:YES];

Upvotes: 1

taskinoor
taskinoor

Reputation: 46037

[myScrollView scrollRectToVisible:myView.frame animated:TRUE];

This will scroll so that myView becomes visible, assuming myView is added to the scroll view and it is not already visible. You need to keep track of current visible view and in the button handler you need to pass next view's frame as parameter to the above method.

Upvotes: 3

Related Questions