【发布时间】:2012-01-28 12:25:10
【问题描述】:
我有一个水平滚动的滚动视图。我只是在其中添加了图像、标签和 web 视图,其中有很多对象。 我想直接滚动到滚动视图中的第一个对象。 我该怎么做?
【问题讨论】:
标签: iphone ios uiscrollview
我有一个水平滚动的滚动视图。我只是在其中添加了图像、标签和 web 视图,其中有很多对象。 我想直接滚动到滚动视图中的第一个对象。 我该怎么做?
【问题讨论】:
标签: iphone ios uiscrollview
使用以下代码:
scrollView.contentOffset = CGPoint(x: 0, y: 0)
【讨论】:
如果有人想知道如何在 Swift 5 中使用 xcode 11:
我在 x 参数中输入负值,因为我想向左滚动
let frame = CGRect(x: -540, y: 0, width: 1080, height: 370); //wherever you want to scroll
boardScrollView.scrollRectToVisible(frame, animated: true) // set animated to true if you want to animate it
【讨论】:
最简单的动画方式 ->
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]
【讨论】:
swift 3 版本的答案是:
scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)
【讨论】:
要为滚动设置动画,请执行以下操作:
CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll
[self.scrollView scrollRectToVisible:frame animated:YES];
【讨论】: