UIPageView控制器淡化背景颜色过渡

时间:2017-02-20 08:49:06

标签: ios swift3 background-color uipageviewcontroller

我正在使用swift 3并希望使用从一个视图控制器背景颜色到另一个视图控制器背景颜色的淡入淡出颜色(页面视图控制器颜色过渡) 例如,如果第一页是蓝色而第二页是红色,当用户滚动到第二页时蓝色过渡到红色,当红色页面完全覆盖屏幕时颜色为红色 我使用了这些代码,但收到了错误

UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.80)
    UIView.setAnimationTransition(.flipFromLeft, for: (self.navigationController?.view)!, cache: false)
    self.navigationController?.pushViewController(self.next as! UIViewController , animated: true)
    UIView.commitAnimations()

以下是我的所有代码

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


    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.80)
    UIView.setAnimationTransition(.flipFromLeft, for: (self.navigationController?.view)!, cache: false)
    self.navigationController?.pushViewController(self.next as! UIViewController , animated: true)
    UIView.commitAnimations()

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

    }

    }
}





}

0 个答案:

没有答案