最简单的解决方案是使用页面视图控制器。当您这样做时,将自动为您调用子项的外观方法(它使您摆脱所有密集的代码来手动填充和配置滚动视图):
class MainViewController: UIPageViewController {
let controllers = [RedViewController(), GreenViewController(), BlueViewController()]
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
setViewControllers([controllers.first!], direction: .forward, animated: true, completion: nil)
}
}
extension MainViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(where: { $0 == viewController }), index < (controllers.count - 1) {
return controllers[index + 1]
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = controllers.firstIndex(where: { $0 == viewController }), index > 0 {
return controllers[index - 1]
}
return nil
}
}
如果您真的想使用滚动视图方法,请不要预先执行视图控制器包含代码,而仅在它们滚动到视图时添加它们(并在它们滚动到视图之外时将它们删除)。你只需要为你的滚动视图设置一个委托并实现一个UIScrollViewDelegate
方法。
因此,例如,我可能只使用这三个子视图控制器的容器子视图填充我的滚动视图。(请注意containerViews
,在下面的示例中只是空白UIView
实例,布置了子视图控制器视图最终将去的位置。)然后我可以查看CGRect
滚动视图的可见部分是否与容器视图相交,并执行视图控制器包含以及时的方式。
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let rect = CGRect(origin: scrollView.contentOffset, size: scrollView.bounds.size)
for (index, containerView) in containerViews.enumerated() {
let controller = controllers[index]
let controllerView = controller.view!
if rect.intersects(containerView.frame) {
if controllerView.superview == nil {
// a container view has scrolled into view, but the associated
// child controller view has not been added to the view hierarchy, yet
// so let's do that now
addChild(controller)
containerView.addSubview(controllerView)
controllerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
controllerView.topAnchor.constraint(equalTo: containerView.topAnchor),
controllerView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
controllerView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
controllerView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor)
])
controller.didMove(toParent: self)
}
} else {
if controllerView.superview != nil {
// a container view has scrolled out of view, but the associated
// child controller view is still in the view hierarchy, so let's
// remove it.
controller.willMove(toParent: nil)
controllerView.removeFromSuperview()
controller.removeFromParent()
}
}
}
}
}
在这两种情况下,您都会在视图出现时收到包含调用。