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];
精彩评论