Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)];
}
like image 933
Sheehan Alam Avatar asked Jul 15 '26 08:07

Sheehan Alam


1 Answers

[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.

like image 184
taskinoor Avatar answered Jul 18 '26 21:07

taskinoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!