页面视图控制器背景颜色淡入淡出转换在swift 3中

时间:2017-02-19 09:15:26

标签: swift3 transition uipageviewcontroller uicolor

我想在我的页面视图控制器中使用背景颜色渐变过渡 - 如图所示,我有5页,最后一页有图片,我想在每个视图控制器页面中使用颜色渐变过渡,所以我想要当用户指向下一页时,当前页面的颜色淡入下一页的颜色

这是图像 enter image description here

这是我的代码

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!")



    } 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()


    for view in self.view.subviews {
        if view is UIScrollView {
            let pageVC = UIPageControl()
            self.pageVC.pageIndicatorTintColor = UIColor.gray
            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 - 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
    }




    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)
    {

        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("It is Done!!!")

    }


}





}

**我需要在我的页面视图控制器中有一些东西看起来像照片数学应用程序(如何使用部分),如果你不明白我下载这个应用程序,并在这个应用程序中看到页面视图控制器**

0 个答案:

没有答案