gpt4 book ai didi

ios - 如何将 UIScrollView 移动一帧的宽度? (Xcode, swift )

转载 作者:行者123 更新时间:2023-11-28 14:03:03 26 4
gpt4 key购买 nike

我正在尝试制作一个使用类似于 Snapchat 功能的应用程序,在该应用程序中,用户可以在水平相邻的 3 个框架之间滑动。一切似乎都正常,但默认情况下,scrollView 会在最左侧显示 View ,并允许用户向右滑动两次以到达最右侧的 View 。我希望 ScrollView 首先显示中间 View ,允许用户向左或向右滑动,但似乎无法做到这一点。提前致谢。

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

scrollView.frame = self.view.bounds

let ViewOne = HomeViewController(nibName: "HomeViewController", bundle: nil)
ViewOne.view.frame = self.view.bounds

let ViewTwo = ExploreViewController(nibName: "ExploreViewController", bundle: nil)
ViewTwo.view.frame = self.view.bounds

let ViewThree = ProfileViewController(nibName: "ProfileViewController", bundle: nil)
ViewThree.view.frame = self.view.bounds

self.addChildViewController(ViewOne)
self.scrollView.addSubview(ViewOne.view)
ViewOne.didMove(toParentViewController: self)

self.addChildViewController(ViewTwo)
self.scrollView.addSubview(ViewTwo.view)
ViewTwo.didMove(toParentViewController: self)

self.addChildViewController(ViewThree)
self.scrollView.addSubview(ViewThree.view)
ViewThree.didMove(toParentViewController: self)

ViewTwo.view.center = CGPoint(x: ViewOne.view.center.x + self.view.frame.width, y: self.view.center.y)
ViewThree.view.center = CGPoint(x: ViewOne.view.center.x - self.view.frame.width, y: self.view.center.y)

scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
print(self.view.center.x)

}

最佳答案

scrollView.contentOffset = CGPoint(x: self.view.bounds.width, y: 0)

这会将 ScrollView 偏移一个等于 View 宽度的量

关于ios - 如何将 UIScrollView 移动一帧的宽度? (Xcode, swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53346062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com