如何以编程方式滚动 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)];
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将滚动,以便
myView
变得可见,假设myView
已添加到滚动视图并且它尚未可见。您需要跟踪当前可见视图,并且在按钮处理程序中,您需要将下一个视图的框架作为参数传递给上述方法。This will scroll so that
myView
becomes visible, assumingmyView
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.