美文网首页
启动时的向导页(新手引导)的制作

启动时的向导页(新手引导)的制作

作者: 焉逢12 | 来源:发表于2017-03-21 16:06 被阅读0次
    import UIKit
     
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
     
        var window: UIWindow?
     
        func application(_ application: UIApplication, didFinishLaunchingWithOptions
            launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            //增加标识,用于判断是否是第一次启动应用...
            if (!(UserDefaults.standard.bool(forKey: "everLaunched"))) {
                UserDefaults.standard.set(true, forKey:"everLaunched")
                let guideViewController = GuideViewController()
                self.window!.rootViewController=guideViewController;
                print("guideview launched!")
            }
            return true
        }
     
        func applicationWillResignActive(_ application: UIApplication) {
        }
     
        func applicationDidEnterBackground(_ application: UIApplication) {
        }
     
        func applicationWillEnterForeground(_ application: UIApplication) {
        }
     
        func applicationDidBecomeActive(_ application: UIApplication) {
        }
     
        func applicationWillTerminate(_ application: UIApplication) {
        }
    }
    
    import UIKit
     
    class GuideViewController:UIViewController,UIScrollViewDelegate
    {
        //页面数量
        var numOfPages = 3
         
        override func viewDidLoad()
        {
            let frame = self.view.bounds
            //scrollView的初始化
            let scrollView = UIScrollView()
            scrollView.frame = self.view.bounds
            scrollView.delegate = self
            //为了能让内容横向滚动,设置横向内容宽度为3个页面的宽度总和
            scrollView.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages),
                                            height:frame.size.height)
            print("\(frame.size.width*CGFloat(numOfPages)),\(frame.size.height)")
            scrollView.isPagingEnabled = true
            scrollView.showsHorizontalScrollIndicator = false
            scrollView.showsVerticalScrollIndicator = false
            scrollView.scrollsToTop = false
            for i in 0..<numOfPages{
                let imgfile = "jianjie\(Int(i+1)).png"
                print(imgfile)
                let image = UIImage(named:"\(imgfile)")
                let imgView = UIImageView(image: image)
                imgView.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(0),
                                       width:frame.size.width, height:frame.size.height)
                scrollView.addSubview(imgView)
            }
            scrollView.contentOffset = CGPoint.zero
            self.view.addSubview(scrollView)
        }
         
        //scrollview滚动的时候就会调用
        func scrollViewDidScroll(_ scrollView: UIScrollView)
        {
            print("scrolled:\(scrollView.contentOffset)")
            let twidth = CGFloat(numOfPages-1) * self.view.bounds.size.width
            //如果在最后一个页面继续滑动的话就会跳转到主页面
            if(scrollView.contentOffset.x > twidth)
            {
                let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
                let viewController = mainStoryboard.instantiateInitialViewController()
                self.present(viewController!, animated: true, completion:nil)
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:启动时的向导页(新手引导)的制作

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