我正在使用一个容器来实现入职流程。这样做,我希望用户仅在显示流程的最后一页时才能使用“下一步”按钮。
当前,按钮将在最后一页即将显示的同时显示(即使仅检测到手势,但页面仍保留在同一页面上,也会显示按钮。我的意思是我有3页如果我愿意交换,如果我停留在第二页上,则显示按钮)。
这是我的课程:
class OnboardingViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
enum PageViews: String {
case firstPV
case secondPV
case thirdPV
}
var skipBtn: UIButton!
var nextIndex: Int!
fileprivate lazy var orderedViewController: [UIViewController] = {
return [self.getViewController(withIdentifier: PageViews.firstPV.rawValue),
self.getViewController(withIdentifier: PageViews.secondPV.rawValue),
self.getViewController(withIdentifier: PageViews.thirdPV.rawValue)]
}()
fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
return (storyboard?.instantiateViewController(withIdentifier: identifier))!
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewController.index(of: viewController) else { return nil }
let previousIndex = viewControllerIndex - 1
enableSkipBtn(enabling: false)
guard previousIndex >= 0 else { return nil }
guard orderedViewController.count > previousIndex else { return nil }
return orderedViewController[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewController.index(of: viewController) else { return nil }
nextIndex = viewControllerIndex + 1
guard nextIndex < orderedViewController.count else { return nil }
guard orderedViewController.count > nextIndex else { return nil }
if nextIndex == orderedViewController.count - 1 {
enableSkipBtn(enabling: true)
}
return orderedViewController[nextIndex]
}
func presentationCount(for: UIPageViewController) -> Int {
return orderedViewController.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firstVC = orderedViewController.first {
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
nextIndex = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: Private Methods
func enableSkipBtn(enabling: Bool) {
skipBtn.isEnabled = enabling
skipBtn.isHidden = !enabling
}
}
我正在使用viewControllerAfter函数隐藏或显示我的按钮,但是它没有做我想做的事情。
如果您需要更多详细信息,请问。
谢谢。
答案 0 :(得分:1)
实施此委托方法:
unique_ptr