[英]How do I scroll to a position programmatically in UIScrollView
我有一个水平滚动的滚动视图。 我只是在其中添加了图像、标签和 webview,其中有很多对象。 我想直接滚动到滚动视图中的第一个对象。 我该怎么做呢?
使用以下代码:
scrollView.contentOffset = CGPoint(x: 0, y: 0)
要为滚动设置动画,请执行以下操作:
CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll
[self.scrollView scrollRectToVisible:frame animated:YES];
最简单的动画方式 ->
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]
答案的 swift 3 版本是:
scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)
如果有人想知道如何使用 xcode 11 在 Swift 5 中做到这一点:
我把负值放在 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.