美文网首页swift干货demosSwift
[iOS] App 引导页的简单实现 (Swift 3)

[iOS] App 引导页的简单实现 (Swift 3)

作者: 老初 | 来源:发表于2015-08-20 17:02 被阅读11559次

** 转载请注明出处:http://www.jianshu.com/p/024dd2d6e6e6#**

已更新至 Xcode8.2、Swift3

在第一次打开App或者App更新后通常用引导页来展示产品特性

我们用NSUserDefaults类来判断程序是不是第一次启动或是否更新,在 AppDelegate.swift中加入以下代码:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // 得到当前应用的版本号
    let infoDictionary = Bundle.main.infoDictionary
    let currentAppVersion = infoDictionary!["CFBundleShortVersionString"] as! String
        
    // 取出之前保存的版本号
    let userDefaults = UserDefaults.standard
    let appVersion = userDefaults.string(forKey: "appVersion")
        
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
        
    // 如果 appVersion 为 nil 说明是第一次启动;如果 appVersion 不等于 currentAppVersion 说明是更新了
    if appVersion == nil || appVersion != currentAppVersion {
        // 保存最新的版本号
        userDefaults.setValue(currentAppVersion, forKey: "appVersion")
            
        let guideViewController = storyboard.instantiateViewController(withIdentifier: "GuideViewController") as! GuideViewController
        self.window?.rootViewController = guideViewController
    }
        
    return true
}

GuideViewController中,我们用UIScrollView来装载我们的引导页:

class GuideViewController: UIViewController {
    
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var startButton: UIButton!
    
    fileprivate var scrollView: UIScrollView!
    
    fileprivate let numOfPages = 3

    override func viewDidLoad() {
        super.viewDidLoad()

        let frame = self.view.bounds
        
        scrollView = UIScrollView(frame: frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        // 将 scrollView 的 contentSize 设为屏幕宽度的3倍(根据实际情况改变)
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height)
        
        scrollView.delegate = self
        
        for index  in 0..<numOfPages {
            let imageView = UIImageView(image: UIImage(named: "GuideImage\(index + 1)"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        
        self.view.insertSubview(scrollView, at: 0)
        
        // 给开始按钮设置圆角
        startButton.layer.cornerRadius = 15.0
        // 隐藏开始按钮
        startButton.alpha = 0.0
    }
    
    // 隐藏状态栏
    override var prefersStatusBarHidden : Bool {
        return true
    }
}

最后我们让GuideViewController遵循UIScrollViewDelegate协议,在这里判断是否滑动到最后一张以显示进入按钮:

// MARK: - UIScrollViewDelegate
extension GuideViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        
        // 因为currentPage是从0开始,所以numOfPages减1
        if pageControl.currentPage == numOfPages - 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.startButton.alpha = 1.0
            }) 
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.startButton.alpha = 0.0
            }) 
        }
    }
}

在上面的代码中,为了显得自然我们给进入按钮加入了一点动画 :]

最终效果如下:


GuideScreenshot.gif

Github地址:https://github.com/GuiminChu/JianshuExample

相关文章

网友评论

  • CHN_Liao:我按照你的例子做了一个demo,给按钮设置圆角时候报fatal error: unexpectedly found nil while unwrapping an Optional value错误,为什么呢,我是新手
    老初:startButton 在 Storyboard 里,你是不是忘了连线了?
  • hsy_song:有oc版的吗 swift用起来不方便啊
  • 4a5a6523f8d2:你好,问下,为什么从第三页往左侧滑动时,那个下面的点点会动呀?而不是像往右滑动时的效果,滑动时不动。
  • 巴图鲁:膜拜
  • FengxinLi:请问一下楼主为什么用的扩展来实现UIScrolView的协议?为什么不直接实现协议呢?
    老初:@Fengxinliju 习惯而已 :blush:
  • 一直很LAZY:第二次进入怎么跳到其他界面
  • 37c7796cd658:非常棒 简单易懂,UIPageControl能让小点跟着手指进行移动就好了,移动到第几页就覆盖到灰色小点上面
  • 拿铁代码:非常感谢, 尤其是开始判断app version那个我以前没想到过,而且觉得这个比用嵌套pageViewController那个简便,求问为什么是用
    self.view.insertSubview(scrollView, atIndex: 0) 而不是
    self.view.addSubView
    37c7796cd658:@拿铁代码 self.view.insertSubview(scrollView, atIndex: 0) 可以指定位置,放到最底层
  • 语安月月鸟:好东西!
  • Mr_Jia:rootView = guideView,引导页面结束怎么跳到主页面
    拿铁代码:@Mr_Jia 挺好呀 这样不用考虑dismiss那个guide viewcontroller,直接决定哪个是rootViewController
    Mr_Jia:@老初 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenLogin"]) {
    //设置窗口控制器
    ViewController *view = [[ViewController alloc] init];
    self.window.rootViewController = view;
    }else{
    GuideViewController *gvc = [[GuideViewController alloc] init];
    self.window.rootViewController = gvc;
    }

    我现在用的这种方法,也可以实现,在引导界面里加一个按钮,present到主界面,但总感觉怪怪的。
    老初:@Mr_Jia 有一个“开始体验”的Button,在里面加个跳转事件
  • 极分享:棒棒哒 转载啦! :smiley:
  • feeef450986b:你好,请问,有源码吗?
    老初:@Leon双龙 感谢关注,已增加源码 :blush:
  • 40bb498a328b:棒棒哒

本文标题:[iOS] App 引导页的简单实现 (Swift 3)

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