我正在为物理治疗诊所的新患者创建一个入职应用程序。新患者将在UIPageViewController
内的每个视图控制器上回答问题,然后点击按钮转到下一个问题。
我按照教程设置了UIPageViewController
PageVC
和3个视图控制器。 PageVC
目前通过滑动更改页面,但我希望能够使用两个按钮作为PageVC
本身的子视图,在视图控制器中前后导航。我该如何做到这一点?
我喜欢使用UIPageViewController
并希望了解它。如果除了使用UIPageViewController
完成同样的任务之外还有一种更有效的方法,我很乐意考虑它。
class PageVC: UIPageViewController, UIPageViewControllerDataSource {
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.VCInstance(name: "BodyChart"),
self.VCInstance(name: "Symptoms"),
self.VCInstance(name: "HadSurgery")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: name)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
}
答案 0 :(得分:1)
我认为您正在寻找的是here.但这是另一种方式。
UIPageViewController有一个实例方法转换。下面的代码段将从第一页子视图控制器移动到第二页子视图控制器。
pageViewController.transition(from: pageViewController.viewControllers[0], to: pageViewController.viewControllers[1], duration: 0.3, options: UIViewAnimationOptions.curveEaseInOut, animations: nil, completion: nil)
PageVC目前通过滑动来更改页面,但我希望能够使用两个按钮在视图控制器中前后导航
将您的按钮添加到网页内容,然后停用horizontal scrolling on PageVC's scrollview或停用视图'用户互动以有礼貌的方式。