页面视图控制器隐藏指示点并在最后一页停止(无法返回)

时间:2017-02-15 10:35:48

标签: ios swift uipageviewcontroller swift3.0.2

我的项目中有一个页面控件,我想做两件事: 我想在最后一页隐藏指示点 2.我想锁定(页面VC(我的班级))中的最后一页,我无法回到第一页 这是我的代码

class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate {

lazy var VCArr : [UIViewController] = {
    return [self.VCInstance(name : "FirtsVC"),
            self.VCInstance(name : "SecondVC"),
            self.VCInstance(name :"ThirdVC"),
            self.VCInstance(name :"FourthVC"),
            self.VCInstance(name :"FivethVC")]
}()

private func VCInstance(name : String) -> UIViewController {
    return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self

    if VCArr.last != nil {
        print("first Page Reached!")
    }

    if let firstVC = VCArr.first {
        setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)
    }
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let pageVC = UIPageControl()

    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        }else if view is UIPageControl{
            view.backgroundColor = UIColor.clear
            pageVC.numberOfPages = 5
            pageVC.center = self.view.center
            pageVC.layer.position.y = self.view.frame.height - 180 ;
        }
    }
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex-1
    guard previousIndex >= 0 else {
        return VCArr.last
    }

    guard VCArr.count > previousIndex else {
        return nil
    }

    return VCArr[previousIndex]
}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex+1
    guard nextIndex < VCArr.count
        else {
        return VCArr.first
    }

    guard VCArr.count > nextIndex else {
        return nil
    }
    return VCArr[nextIndex]
}


 public func presentationCount(for pageViewController: UIPageViewController) -> Int{
    return VCArr.count
}

 public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
    guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
        return 0
    }

    return firstViewControllerIndex
}
public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if (VCArr.last!.isViewLoaded)
    {
        print("It is Done!!!")
    }
    }

1 个答案:

答案 0 :(得分:0)

更改此选项,这使得页面视图在最后一个页面视图中返回到第一个页面视图:

let nextIndex = viewControllerIndex+1
guard nextIndex < VCArr.count
    else {
    return nil //was VCArr.first
}

要隐藏它,首先将let pageVC = UIPageControl()移到class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate {下方,使其在课程中公开。然后添加:

if nextIndex == VCArr.count{
    pageVC.isHidden = true
}