如何以编程方式滚动UIScrollView?

时间:2011-04-20 16:38:45

标签: iphone objective-c cocoa-touch xcode uiscrollview

我有一个UIScrollView,我可以滑动以显示新视图。我有一个按钮,我想以编程方式滚动浏览视图。因此,如果我单击一次,它将滚动到下一个视图。我该如何实现呢?

这是滚动视图代码

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

2 个答案:

答案 0 :(得分:3)

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

这将滚动以便myView变为可见,假设myView已添加到滚动视图但尚未显示。您需要跟踪当前的可见视图,并在按钮处理程序中将下一个视图的帧作为参数传递给上述方法。

答案 1 :(得分:1)

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

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