如何在 SWIFT 中从尾页开始水平滚动视图
How to start horizontal scroll view from end page in SWIFT
我有一个水平滚动视图并且启用了分页选项。所以我在 3 个页面中添加了总共 3 个图像视图 "horizontally"。我希望我的最后一页首先显示。即从右到右开始。这是我的代码:
self.myScrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
let scrollViewWidth = self.myScrollView.frame.width
let scrollViewHeight = self.myScrollView.frame.height
let imgOne = UIImageView(frame: CGRectMake(0, 0, scrollViewWidth, scrollViewHeight))
let imgTwo = UIImageView(frame: CGRectMake(-(scrollViewWidth), 0, scrollViewWidth, scrollViewHeight))
let imgThree = UIImageView(frame: CGRectMake(-(scrollViewWidth)*2, 0, scrollViewWidth, scrollViewHeight))
imgOne.image = UIImage(named: "0")
imgTwo.image = UIImage(named: "1")
imgThree.image = UIImage(named: "2")
self.myScrollView.addSubview(imgOne)
self.myScrollView.addSubview(imgTwo)
self.myScrollView.addSubview(imgThree)
self.myScrollView.contentSize = CGSizeMake(scrollViewWidth*3, 0)
最初将 contentOffset
设置为 scrollViewWidth*2
,这将水平滚动到两个页面:
scrollView.contentOffset = CGPointMake(scrollViewWidth*2,0)
并且您可以在 X
值中设置任何您想要的值以水平滚动。
我有一个水平滚动视图并且启用了分页选项。所以我在 3 个页面中添加了总共 3 个图像视图 "horizontally"。我希望我的最后一页首先显示。即从右到右开始。这是我的代码:
self.myScrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
let scrollViewWidth = self.myScrollView.frame.width
let scrollViewHeight = self.myScrollView.frame.height
let imgOne = UIImageView(frame: CGRectMake(0, 0, scrollViewWidth, scrollViewHeight))
let imgTwo = UIImageView(frame: CGRectMake(-(scrollViewWidth), 0, scrollViewWidth, scrollViewHeight))
let imgThree = UIImageView(frame: CGRectMake(-(scrollViewWidth)*2, 0, scrollViewWidth, scrollViewHeight))
imgOne.image = UIImage(named: "0")
imgTwo.image = UIImage(named: "1")
imgThree.image = UIImage(named: "2")
self.myScrollView.addSubview(imgOne)
self.myScrollView.addSubview(imgTwo)
self.myScrollView.addSubview(imgThree)
self.myScrollView.contentSize = CGSizeMake(scrollViewWidth*3, 0)
最初将 contentOffset
设置为 scrollViewWidth*2
,这将水平滚动到两个页面:
scrollView.contentOffset = CGPointMake(scrollViewWidth*2,0)
并且您可以在 X
值中设置任何您想要的值以水平滚动。