美文网首页
引导页

引导页

作者: 宁静1致远 | 来源:发表于2017-08-17 13:05 被阅读0次

    引导页是用户第一次使用app时,引导用户使用的页面,这个界面通常加载到进入界面的上面。
    我这个引导页是一个View,这个View加载到进入界面View的上面,当然你也可以加载到window上,但是最好还是加载到View上较好一下,下面直接上代码:

    import UIKit
    
    enum GoinTpye {
        case scrollGoin
        case clickGoin
    }
    
    typealias JNGuideViewCallback = ()->()
    class JNGuideView: UIView ,UIScrollViewDelegate,UIGestureRecognizerDelegate{
        var guideSkipBack:JNGuideViewCallback?
        var goinTpye:GoinTpye = GoinTpye.scrollGoin//进入类型
        
       private let SCREENHEIGHT = UIScreen.mainScreen().bounds.size.height
       private let SCREENWEIGHT = UIScreen.mainScreen().bounds.size.width
       private let isFirstUseApp:String = "isFirstUserApp"//判断是否为第一次登陆的key
       private var pageControl:UIPageControl?
        
        //重写init方法
        override init(frame: CGRect) {
            super.init(frame: frame)
            guideView()
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        func guideView() -> () {
            if(!NSUserDefaults.standardUserDefaults().boolForKey(isFirstUseApp)){//是第一次使用
                NSUserDefaults.standardUserDefaults().setBool(true, forKey: isFirstUseApp)
                //创建guideView子视图
                self.initSubViews()
            }else{
                UIView.animateWithDuration(0, animations: {
                }) { (Bool) in
                    self.guideSkipBack!()
                }
            }
        }
        //加载导航页
        func initSubViews() -> () {
            var guideStr:String
            switch SCREENHEIGHT {
            case 480:guideStr = "-4@2x.jpg"
            case 568:guideStr = "-5@2x.jpg"
            case 667:guideStr = "-6@2x.jpg"
            case 736:guideStr = "-6p@3x.jpg"
            default:guideStr = "-4@2x.jpg"
            }
            let guideArray = ["GuidePage01"+guideStr,"GuidePage02"+guideStr] //这里是图片,需要自己添加
            //创建滚动试图
            let scroll:UIScrollView = UIScrollView.init(frame: CGRect(x: 0,y: 0,width:SCREENWEIGHT ,height: SCREENHEIGHT ))
            scroll.showsVerticalScrollIndicator = false
            scroll.showsHorizontalScrollIndicator = false
            scroll.directionalLockEnabled = false
            scroll.pagingEnabled = true
            scroll.bounces = false
    //        scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
            scroll.delegate = self
            
            //创建page视图
            pageControl = UIPageControl.init(frame: CGRect(x:0 ,y: SCREENHEIGHT - 70,width:SCREENWEIGHT,height:50))
            pageControl?.alpha = 0.5
            pageControl?.numberOfPages = guideArray.count
            pageControl?.currentPage = 0
            pageControl?.pageIndicatorTintColor = UIColor.darkGrayColor()
            pageControl?.currentPageIndicatorTintColor = UIColor.redColor()
            
            for index in 0..count {
                var guideImageView:UIImageView = UIImageView.init(frame: CGRect(x: CGFloat(index) * SCREENWEIGHT,y: 0,width: SCREENWEIGHT,height: SCREENHEIGHT))
                guideImageView.userInteractionEnabled = true
                guideImageView.tag = index + 1
                guideImageView.image = UIImage.init(named: guideArray[index])
                scroll.addSubview(guideImageView)
                if (index == guideArray.count - 1) {//证明是最后一张,
                    switch goinTpye{
                    case .clickGoin://点击进程序
                        var goinBtn:UIButton = UIButton.init(type: UIButtonType.Custom)
                        goinBtn.frame = CGRect(x: (SCREENWEIGHT - 150)/2,y: SCREENHEIGHT - 300,width: 150,height: 100)
                        goinBtn.backgroundColor = UIColor.brownColor()
                        goinBtn.addTarget(self, action: "goin", forControlEvents: UIControlEvents.TouchUpInside)
                        goinBtn.setTitle("进入主界面", forState: UIControlState.Normal)
                        goinBtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
                        guideImageView.addSubview(goinBtn)
                    default://滑动进程序
                        let swip:UISwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: "goin")
                        swip.direction = UISwipeGestureRecognizerDirection.Left
                        swip.delegate = self
                        guideImageView.addGestureRecognizer(swip)
                    }
                }
            }
            scroll.contentSize = CGSize(width: SCREENWEIGHT * CGFloat(guideArray.count),height:0)
            self.addSubview(scroll)
            self.addSubview(pageControl!)
        }
        func goin()->(){
            var imageView:UIImageView = (self.viewWithTag(2) as! UIImageView)
            UIView.animateWithDuration(0.3, animations: {
                imageView.frame = CGRect(x: 0,y: 0,width: self.SCREENWEIGHT,height: self.SCREENHEIGHT)
                }) { (Bool) in
                    self.guideSkipBack!()
            }
        }
    //MARK:- 滚动协议方法
        func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
            let index = scrollView.contentOffset.x / scrollView.frame.size.width;
            pageControl?.currentPage = Int(index)
        }
    //    MARK: - 手势协议方法
        //让多个手势共存
        func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    }
    

    上述代码是滑动进入界面,也可以写点击进入界面,把进入类型换一下就OK了。

    进入界面的调用代码如下:

        override func viewDidLoad() {
            super.viewDidLoad()
            initGuideView();
        }
        //添加引导页试图
        func initGuideView() -> () {
            var guideView:JNGuideView = JNGuideView.init(frame:self.view.bounds)
            weak var weakGuideView = guideView
            guideView.guideSkipBack = {
                weakGuideView!.removeFromSuperview()
                weakGuideView = nil
            }
            self.view.addSubview(guideView)
            self.view.bringSubviewToFront(guideView)
        }
    

    相关文章

      网友评论

          本文标题:引导页

          本文链接:https://www.haomeiwen.com/subject/qywgrxtx.html