简体   繁体   中英

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)];
}
[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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM