1

I am making an iPhone app which has a scrollview.

I have a button on left and right side of the scrollview. On click of these two buttons the scrollview should scroll in the respective horizontal direction.

What should I do to scroll the scroll view on the click of these buttons?

3 Answers 3

4
- (void)scrollRectToVisible:(CGRect)rect  animated:(BOOL)animated
3

given a scrollview called scrollView, call

NSInteger page = ..// determine page
CGPoint off = CGPointMake(page*scrollView.frame.size.width,0);
[scrollView setContentOffset:off animated:YES];

you could determine the current page by calculating

NSInteger page = scrollView.contentOffset.x/scrollView.frame.size.width;

Then page page++; before setting the new offset.

1

sample code:

float width = scrollView.frame.size.width;
float height = scrollView.frame.size.height;
float newPosition = scrollView.contentOffset.x+width; // or -
// probably check if position is negative
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[scrollView scrollRectToVisible:toVisible animated:YES];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.