关闭

swift+UIPageViewController 纯代码实现引导页

标签: swift引导页
1684人阅读 评论(0) 收藏 举报
分类:

使用UIPageViewController

1、介绍:

UIPageViewController是一个类似UINavigationController的Controller容器。它既可以实现UIScrollView的滑动效果,也可以实现UIPageController的翻页效果。其中每个具体的视图由各自的ViewController进行维护管理,UIPageViewController只进行协调与动画布置。
UIPageViewControllerDataSource协议为UIPageViewController提供数据,UIPageViewControllerDelegate对翻页动作、屏幕旋转进行监听。

2、代码:
/// 引导页
class YSGuidePageViewController: UIPageViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {

    /// 页面数量改变时的回调
    var pageViewControllerUpdatePageCount:((YSGuidePageViewController,NSInteger)->Void)!
    /// 页面改变时的回调
    var pageViewControllerUpdatePageIndex:((YSGuidePageViewController,NSInteger)->Void)!

    private(set) lazy var allViewControllers:[UIViewController]={
        return [YSGuideImgViewController(imgName: "guide_pic_new1",frame: self.view.bounds, showBtn:false),
                YSGuideImgViewController(imgName: "guide_pic_new2",frame: self.view.bounds, showBtn:false),
                YSGuideImgViewController(imgName: "guide_pic_new3",frame: self.view.bounds,showBtn:true)]
    }()

    internal init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?, pageCount:((YSGuidePageViewController,NSInteger)->Void)!, pageIndex:((YSGuidePageViewController,NSInteger)->Void)!) {
        super.init(transitionStyle: style, navigationOrientation: navigationOrientation, options: options)
        self.pageViewControllerUpdatePageCount = pageCount
        self.pageViewControllerUpdatePageIndex = pageIndex
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        dataSource = self
        //设置首页
        if let firstViewController = allViewControllers.first {
            setViewControllers([firstViewController], direction: .Forward, animated: true, completion:nil)
        }

        //页面数量改变时
        if self.pageViewControllerUpdatePageCount != nil {
            self.pageViewControllerUpdatePageCount(self,allViewControllers.count)
        }
    }

    //mark:获取前一个页面
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        //guard 条件为false就执行else里的函数块
        guard let viewControllerIndex = allViewControllers.indexOf(viewController) else {
            return nil
        }
        let previousIndex = viewControllerIndex - 1
        guard previousIndex >= 0 else {
            return nil
        }
        guard allViewControllers.count > previousIndex else {
            return nil
        }
        return allViewControllers[previousIndex]
    }

    //mark:获取后一个页面
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = allViewControllers.indexOf(viewController) else {
            return nil
        }
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = allViewControllers.count
        guard orderedViewControllersCount != nextIndex else {
            return nil
        }
        guard orderedViewControllersCount>nextIndex else {
            return nil
        }
        return allViewControllers[nextIndex]
    }

    //mark:页面切换完毕
    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if let firstViewController = viewControllers?.first,let index = allViewControllers.indexOf(firstViewController) {
            if self.pageViewControllerUpdatePageIndex != nil {
                self.pageViewControllerUpdatePageIndex(self,index)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}





/// 具体承载引导图片的VC
class YSGuideImgViewController: UIViewController {

    private(set) lazy var imgview:UIImageView = {
        return UIImageView.init(frame:self.view.bounds)
    }()

    private(set) lazy var beginBtn:UIButton = {
        return UIButton(type:.Custom)
    }()
    /**
     重写了初始化方法

     - parameter imgName: 使用到的图片名称
     - parameter frame:   图片的frame
     - parameter showBtn: 是否展示开始体验的按钮

     - returns:
     */
    init(imgName:String, frame:CGRect, showBtn:Bool){
        super.init(nibName: nil, bundle: nil)
        self.view.frame = frame
        self.view.addSubview(self.imgview)
        self.imgview.image = UIImage(named: imgName)

        self.beginBtn.frame = CGRectMake((self.view.bounds.width-174)/2, self.view.bounds.height-40-42, 174, 42)
        self.beginBtn.setImage(UIImage(named:"home_btn_startup"), forState: .Normal)
        self.beginBtn.addTarget(self, action:#selector(beginBtnClicked), forControlEvents: .TouchUpInside)
        self.view.addSubview(self.beginBtn)
        self.beginBtn.hidden = !showBtn
    }

    /**
     按钮点击事件
     */
    func beginBtnClicked() {
        NSNotificationCenter.defaultCenter().postNotificationName("beginExperience", object: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}





/// 加载PageViewController的VC
class YSGuideViewController: YSBaseController {

    private(set) lazy var pageControl:UIPageControl = {
        return UIPageControl(frame:CGRectMake((self.view.bounds.width-200)/2,self.view.bounds.height-20-10,200,20))
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(removeGuidePageView), name: "beginExperience", object: nil)

        self.view.backgroundColor = UIColor(patternImage:UIImage(named: "launch_bg")!)
        self.view.addSubview(pageControl)
        pageControl.backgroundColor = UIColor.clearColor()
        pageControl.pageIndicatorTintColor = UIColor.whiteColor()
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        loadGuidePageView()
    }

    func loadGuidePageView() {

        let guideview = YSGuidePageViewController.init(transitionStyle: .PageCurl, navigationOrientation: .Horizontal, options: nil, pageCount: { (guide:YSGuidePageViewController, count:NSInteger) in
            self.pageControl.numberOfPages = count
            },pageIndex: {(guide:YSGuidePageViewController, index:NSInteger) in
                self.pageControl.currentPage = index
        })//Scroll
        guideview.view.frame = self.view.bounds
        self.view.addSubview(guideview.view)
        self.addChildViewController(guideview)

        self.view.bringSubviewToFront(pageControl)
    }


    /**
     移除引导页,同时记录引导页已经显示完成
     */
    func removeGuidePageView() {

        self.view.removeFromSuperview()
        self.removeFromParentViewController()
        //Todo:存储第一次已经显示引导页

    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
3、实现提醒:

1、首先使用的过程中出现的一个最大最无语的问题就是UIPageViewController的代理不执行,而且只有一个引导页。
出现这个问题的原因是在YSGuideViewController中使用的时候忘记写 self.addChildViewController(guideview) ,所以这个狠重要。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:50977次
    • 积分:1030
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类