简体   繁体   English

以编程方式滚动UIScrollView

[英]Scroll a UIScrollView programmatically

In my iPhone application, I have a UIScrollview with five pages. 在我的iPhone应用程序中,我有一个包含五页的UIScrollview。 Is that possible to tap a button and scroll the pages? 是否可以点击按钮并滚动页面? for example, I am in the first page and if I press the "Button 5" the fifth page should come over like you scrolling with your hand. 例如,我在第一页,如果我按下“按钮5”,第五页应该像你用手滚动一样过来。

Is this possible? 这可能吗?

Sure it is possible! 当然有可能! Check the method - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated . 检查方法- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated When you press the button, call it with the point offset (x and y) and set animated to YES. 按下按钮时,使用点偏移(x和y)调用它并将动画设置为YES。

Also, if you are using a UIScrollView, it is very recommended by Apple's Human Interface Guidelines that you use a UIPageControl, that is good for two reasons: (1) shows the user in which page he is, and (2) can tell your view controller if it was tapped (however, as the doc says, "the page control advances only one page in either direction" , so if you need to jump, let's say, from page 2 to 5, maybe would be better to use buttons). 此外,如果您使用的是UIScrollView,Apple的人机界面指南建议您使用UIPageControl,这有两个原因:(1)向用户显示他所在的页面,(2)可以告诉您查看控制器,如果它被点击(但是,正如文档所说, “页面控件只向任一方向推进一页” ,所以如果你需要跳转,让我们说,从第2页到第5页,也许更好的使用按钮)。

you can do this by this one line code. 你可以通过这一行代码来做到这一点。

[scrollView scrollRectToVisible:frame animated:YES];

here you need to provide frame to it that wich part of scrollview you want to show. 在这里你需要为它提供框架,你要显示的是scrollview的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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