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

我有一个水平滚动的滚动视图。 我只是在其中添加了图像,标签和Web视图,并且其中包含许多对象。我想直接滚动到滚动视图中的第一个对象。我该怎么做呢?

user1072740 asked 2020-02-29T09:52:52Z
4个解决方案
89 votes

使用以下代码:

scrollView.contentOffset = CGPointMake(0,0);
Saurabh Passolia answered 2020-02-29T09:54:48Z
45 votes

要使滚动动画,请执行以下操作:

CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll 
[self.scrollView scrollRectToVisible:frame animated:YES];
inorganik answered 2020-02-29T09:55:08Z
3 votes

动画的最简单方法->

[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]
Kakshil Shah answered 2020-02-29T09:55:28Z
0 votes

答案的迅速3版本是:

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

Arjun Nagineni answered 2020-02-29T09:55:52Z
translate from https://stackoverflow.com:/questions/8644922/how-do-i-scroll-to-a-position-programmatically-in-uiscrollview