UIpageControl指标点

时间:2017-02-18 06:20:22

标签: swift3 uipageviewcontroller

我有一个指针Dots消失的页面控制器!我想在最后一页消失它们! 我也有另一个问题 我想从底部移动这些点+100! 我的最后一个问题:哪种方法可以帮助我在页面视图控制器的任何页面中编写任何方法? (我想在用户看到那些页面时控制所有页面)

这是我的代码

class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate {



let pageVC = UIPageControl()
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.first != 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 {
            let pageControl = UIPageControl()
            pageControl.pageIndicatorTintColor = UIColor.gray
            pageControl.currentPageIndicatorTintColor = UIColor.white
            pageControl.backgroundColor = UIColor.darkGray
            pageControl.numberOfPages = VCArr.count
            pageControl.center = self.view.center
            self.view.addSubview(pageControl)
            pageControl.layer.position.y = self.view.frame.height - 100;
        }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 nil
    }

    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 nil
    }

    guard VCArr.count > nextIndex else {
        return nil
    }
    if nextIndex == VCArr.count{
        pageVC.isHidden = false

    }



    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
    }
        let firstIndex = firstViewControllerIndex - 1
        guard firstIndex >= VCArr.count  else {
            return VCArr.count



    }

    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)

这是答案

class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate {



let pageVC = UIPageControl()
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



    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
    if launchedBefore  {
        print("Not first launch!")
        print("Should Launch MainTabBarController!")

        /////Not First Time launch! reach mainTabBarController//////////



    } else {
        print("First launch, setting UserDefault.")
        UserDefaults.standard.set(true, forKey: "launchedBefore")
    }
    //////Lock Rotation/////////
    var shouldAutorotate: Bool {
        return false
    }
    var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let value =  UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    }
    func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.portrait ]
    }
    ///////Lock Roration//////
    if VCArr.first != nil {

        print("first Page Reached!")
    }

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

    }


}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let view = self.pageVC.currentPage
    for view in self.view.subviews {
        if view is UIScrollView {
            let pageVC = UIPageControl()
            self.pageVC.pageIndicatorTintColor = UIColor.lightText
            self.pageVC.currentPageIndicatorTintColor = UIColor.white
            self.pageVC.backgroundColor = UIColor.white
            self.pageVC.numberOfPages = VCArr.count
            self.pageVC.center = self.view.center
            self.view.addSubview(self.pageVC)
            pageVC.layer.position.y = self.view.frame.height - 150;
        }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 - 100 ;

        }
    }

}




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 nil
    }

    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 nil
    }

    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
    }
        let firstIndex = firstViewControllerIndex - 1
        guard firstIndex >= VCArr.count  else {
            return VCArr.count



    }

    return firstViewControllerIndex
}
public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    self.pageVC.currentPage = pageViewController.viewControllers!.first!.view.tag

 if(viewControllers!.first?.view.tag == 4 ) {

    if (VCArr.last!.isViewLoaded)

    {

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "FivethVC")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = viewController
        print("Last Page Reached!!!")

    }

    }
}





}