[英]How do I scroll to a position programmatically in UIScrollView
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.
我只是在其中添加了图像、标签和 webview,其中有很多对象。 I want to directly scroll to the first object in the scroll view.
我想直接滚动到滚动视图中的第一个对象。 How do I do this?
我该怎么做呢?
使用以下代码:
scrollView.contentOffset = CGPoint(x: 0, y: 0)
To animate your scrolling do this:要为滚动设置动画,请执行以下操作:
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)
If someone wonder how to do it in Swift 5 with xcode 11:如果有人想知道如何使用 xcode 11 在 Swift 5 中做到这一点:
I put negative value in the x parameters becouse I want to scroll to left我把负值放在 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.