如何在UIScrollView中以编程方式滚动到某个位置

时间:2022-12-11 07:32:26

I have a scroll view which scrolls horizontally. I just added images, labels and a webview in it and there are many objects in it. I want to directly scroll to the first object in the scroll view. How do I do this?

我有一个水平滚动的滚动视图。我只是在里面添加了图片、标签和webview,里面有很多东西。我想直接滚动到滚动视图中的第一个对象。我该怎么做呢?

4 个解决方案

#1


87  

Use the following code:

使用下面的代码:

scrollView.contentOffset = CGPointMake(0,0);

#2


44  

To animate your scrolling do this:

为了让你的滚轴动起来,你可以这样做:

CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll 
[self.scrollView scrollRectToVisible:frame animated:YES];

#3


3  

Easiest way with animation ->

最简单的方式与动画->

[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]

#4


0  

The swift 3 version of the answer is:

swift 3版本的答案是:

scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)

滚动视图。contentOffset = CGPoint(x: 0.0, y: 0.0)

#1


87  

Use the following code:

使用下面的代码:

scrollView.contentOffset = CGPointMake(0,0);

#2


44  

To animate your scrolling do this:

为了让你的滚轴动起来,你可以这样做:

CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll 
[self.scrollView scrollRectToVisible:frame animated:YES];

#3


3  

Easiest way with animation ->

最简单的方式与动画->

[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]

#4


0  

The swift 3 version of the answer is:

swift 3版本的答案是:

scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)

滚动视图。contentOffset = CGPoint(x: 0.0, y: 0.0)