Swift-UIPageViewController的使用

import UIKit

class DWBTestViewController: UIViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {
    ///懒加载一个page
    private lazy var pageViewControllerMain:UIPageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    //数组
    private lazy var dataSouceControll = [UIViewController]()
    //获取索引
    private lazy var nextIndex : Int = 0
    //创建上面的滚动条
    private lazy var scrollerView : UIScrollView = UIScrollView()
    
    //子控制器1
    private lazy var pageFirst : UIViewController = UIViewController()
    //子控制器2
    private lazy var pageTwo : UIViewController = UIViewController()
    //子控制器3
    private lazy var pageThree : UIViewController = UIViewController()
    //子控制器4
    private lazy var pagefouer : UIViewController = UIViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        //创建导航栏
       createSelfNav()
        
        //创建UI
       createPageUI()
        
    }
    
    /// 将要出现的时候影藏系统导航栏,这里创建导航栏
    func createSelfNav(){
        //导航栏父视图
        let topView = UIView()
        topView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 64)
        topView.backgroundColor = UIColor.white
        self.view.addSubview(topView)
        
        //创建返回键
        let buttonBack:UIButton = UIButton(type: .custom)
        buttonBack.frame = CGRect(x:0,y:20,width:44,height:44)
        buttonBack.setImage(UIImage(named: "返回"), for: .normal)
        buttonBack.setTitleColor(UIColor.yellow, for: .normal)
        buttonBack.addTarget(self, action:#selector(blaceAction), for: .touchUpInside)
        topView.addSubview(buttonBack)
        
        //创建标题
        let labelTitle = UILabel()
        labelTitle.frame = CGRect(x: 44, y: 20, width: SCREEN_WIDTH-88, height: 44)
        labelTitle.text = "UIPageViewController"
        labelTitle.font = UIFont.systemFont(ofSize: 18)
        labelTitle.textColor = UIColor.black
        labelTitle.textAlignment = .center
        topView.addSubview(labelTitle)
        
        
        //创建返回键
        let buttonBackRight:UIButton = UIButton(type: .custom)
        buttonBackRight.frame = CGRect(x:SCREEN_WIDTH-44,y:20,width:44,height:44)
        buttonBackRight.setImage(UIImage(named: "悬浮分享"), for: .normal)
        buttonBackRight.setTitleColor(UIColor.yellow, for: .normal)
        buttonBackRight.tag = 112222
        buttonBackRight.addTarget(self, action:#selector(blaceRightAction), for: .touchUpInside)
        topView.addSubview(buttonBackRight)
        
        
        //创建一条线
        let imageLine = UIImageView()
        imageLine.frame = CGRect(x: 0, y: 63, width: SCREEN_WIDTH, height: 1)
        imageLine.backgroundColor = UIColor.groupTableViewBackground
        topView.addSubview(imageLine)
        
        
    }
    
    //返回点击
    func blaceAction(){
        
        self.navigationController!.popViewController(animated: true)
    }
    
    //分享
    func blaceRightAction() -> Void{
        
    }
    
    
    func createPageUI() {
        
        pageViewControllerMain.view.frame = CGRect(x: 0, y: 64+33, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64-33)
        pageViewControllerMain.delegate = self
        pageViewControllerMain.dataSource = self
        addChildViewController(pageViewControllerMain)
        view.addSubview(pageViewControllerMain.view)
        pageViewControllerMain.didMove(toParentViewController: self)
        
        //添加字控制器
        dataSouceControll += [pageFirst,pageTwo,pageThree,pagefouer]
        
//   UIPageViewControllerNavigationDirection调用了此方法切换Controller的动画可以为从左往右和从右往左两种,如果animated属性设置为NO,则这个属性无效。是枚举类型,定义了以下两种通过此方法跳转的翻页方式。     
        //从左往右(或从下往上):UIPageViewControllerNavigationDirectionForward;
        //从左往右(或从下往上): UIPageViewControllerNavigationDirectionReverse;
        //设置默认显示控制器
        pageViewControllerMain.setViewControllers([pageFirst], direction:.forward, animated: false, completion: nil)
        view.gestureRecognizers = pageViewControllerMain.gestureRecognizers
        
///MARK--创建标题栏部分
        scrollerView.frame = CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: 33)
        scrollerView.showsVerticalScrollIndicator = false
        scrollerView.showsHorizontalScrollIndicator = false
        view.addSubview(scrollerView)
        //设置滚动视图尺寸
        scrollerView.contentSize = CGSize(width: SCREEN_WIDTH, height: 33)
        
        //设置滚动标题
        let arrayTitle = ["精选","直播","美妆","美搭"]
        for i in 0.. UIViewController?
    {
        
        let index = self.dataSouceControll.index(of: viewController)
        
        if (index == 0) || (index == NSNotFound) {
            return nil
        }
        //守护,防止空值在解包时崩溃
        guard let index1 = index else {
            return nil
        }
         // 此处index必须减一
        return dataSouceControll[index1 - 1]
    }
    
    
     //往左边滑动翻页会走此方法(类似push下去)
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        //获取当前控制器索引
        let index = self.dataSouceControll.index(of: viewController)
        
        if index == NSNotFound {
            return nil
        }
        
        if (index == self.dataSouceControll.count-1) {
            //防止越界,必须要设置
            return nil
        }
        //守护,防止空值在解包时崩溃
        guard let index1 = index else {
            return nil
        }
        
         return dataSouceControll[index1+1]
    }
    
//   取消pageControl,想要取消白点显示,只要不设定以下dataSource方法即可。
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
    {
        return self.dataSouceControll.count
    }
    //跟页码有关
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
    {
        return 0
    }
    
//    下面一个问题,就是如何获取下一个控制器的索引, 在上面两个数据源方法里无法获取准确的索引, 而应该在下面这个方法里获取:
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        let controller = pendingViewControllers[0]
        self.nextIndex = self.dataSouceControll.index(of: controller)!
    }
    
     //还有一个非常有用的代理,它在动画执行完毕后被调用,在controller切换完成后,我们可以在这个代理中进行一些后续操作。例如用UIPageViewController实现轮播分页等功能。
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        
        if completed {
            
            print(self.nextIndex)
            
            //找到所有buton
            for i in 0..

你可能感兴趣的:(Swift-UIPageViewController的使用)