我已经通过一些技巧解决了我的情况。对于每个ContentView
,我都有一个用于缩放的UIImageView
内。UIScrollView
我的问题是,在启动应用程序时,如果用户在滑动之前进行了缩放,那么在放大时转到下一页不会很好。我使用以下代码(Swift 1.2)来解决这个问题。正如我所说,这有点像黑客攻击。
var layoutsubs = false
override func viewDidLoad() {
super.viewDidLoad()
//Other code for implementing pageViewController omitted
//add pageViewController to main view
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
pageViewController.didMoveToParentViewController(self)
//Load to the viewController after the starting VC, then go back to the starting VC
var viewControllers = [afterVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
viewControllers = [startingVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Reverse, animated: true, completion: nil)
}
override func viewWillLayoutSubviews() {
//Load the viewController before the starting VC then go back to the starting VC
//viewWillLayoutSubviews() is called multiple times, so do this only once
if !layoutsubs {
let startingVC = self.viewControllerAtIndex(imageIndex) as ContentViewController
let beforeVC = pageViewController(pageViewController, viewControllerBeforeViewController: startingVC) as! ContentViewController
var viewControllers = [beforeVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Reverse, animated: true, completion: nil)
viewControllers = [startingVC]
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
layoutsubs = true
}
}
本质上,我在启动视图控制器之前和之后加载视图控制器。我通过将每个设置为 VC 以查看通过setViewControllers(_:direction:animated:completion:)
(请参阅 ref),然后返回到起始视图控制器来做到这一点。为什么这是两个不同的功能?好吧,如果你把它们合二为一,那么只有启动 VC 旁边的两个视图控制器中的一个会加载。这在某些情况下可能是可取的,但我需要加载所有三个 VC(之前、启动和之后)。
UIPageViewController
如果已经加载,我不确定这种方法的效果如何。例如,如果您需要在几次滑动后将页面 2 加载到远离正在查看的页面的位置。如果你把它放进去,它可能会跳来跳去willTransitionToViewControllers()
。