如何以编程方式滚动 UIScrollView?

发布于 2024-11-02 06:36:25 字数 499 浏览 0 评论 0原文

我有一个 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)];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-11-09 06:36:25
[myScrollView scrollRectToVisible:myView.frame animated:TRUE];

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

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

月下凄凉 2024-11-09 06:36:25
[myScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0)animated:YES];

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

[myScrollView scrollRectToVisible:self.view.frame animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文