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

cnjp1d6j  于 2023-06-25  发布在  iOS
关注(0)|答案(6)|浏览(201)

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

ejk8hzay

ejk8hzay1#

使用以下代码:

scrollView.contentOffset = CGPoint(x: 0, y: 0)
twh00eeo

twh00eeo2#

要设置滚动的动画,请执行以下操作:

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

c9x0cxw03#

最简单的动画方式->

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

ftf50wuq4#

Swift 3版本的答案是:
scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)

zvms9eto

zvms9eto5#

如果有人想知道如何在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
91zkwejq

91zkwejq6#

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        scrollView.setContentOffset(CGPoint(x: 0.0, y: 350.0), animated: true)
    }

相关问题