Swift3.0 iOS 引导页

iOS 手机应用中常见的引导页面
这里在 Xcode8 上使用 Swift3.0 语言编写

1、 首先在 AppDelegate.swift 中写入判断 App 是否是第一次打开的代码,如果是则进入引导页面,否则进入主页面。

不多说,直接上代码:

AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// AppDelegate.swift
// Diary
//
// Created by moxiaohao on 16/9/27.
// Copyright © 2016年 moxiaohao. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//添加标识,用于判断是否是第一次启动应用...
buildKeyWindow()
return true
}
private func buildKeyWindow() {
window = UIWindow(frame: (self.window?.bounds)!)
window!.makeKeyAndVisible()
if (UserDefaults.standard.object(forKey: "isFristOpenApp") == nil) {
window?.rootViewController = GuideViewController()
UserDefaults.standard.set("fristTimeOpenApp", forKey: "isFristOpenApp")
}else {
let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
window?.rootViewController = mainStoryboard.instantiateInitialViewController()
}
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}



2、 然后到了引导页面的简单设计,这里使用 ScrollView ,使用四张图片,滑动到最后一张图片时显示“欢迎体验”按钮,点击按钮就进入到主页面
GuideViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// GuideViewController.swift
// Diary
//
// Created by moxiaohao on 2016/10/12.
// Copyright © 2016年 moxiaohao. All rights reserved.
//
import UIKit
class GuideViewController: UIViewController,UIScrollViewDelegate {
var scrollView: UIScrollView!
var pageControl: UIPageControl!
var startBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//引导页面 函数调用
guideViewLayout()
}
//scrollview 委托
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let frame = self.view.bounds
let offset = scrollView.contentOffset
pageControl.currentPage = Int(offset.x/frame.size.width)
//按钮出现时的一点点特效
if(pageControl.currentPage == 3) {
UIView.animate(withDuration: 0.5) {
self.startBtn.alpha = 1.0
}
}else{
UIView.animate(withDuration: 0.2) {
self.startBtn.alpha = 0.0
}
}
}
//载入自定义引导页面布局
func guideViewLayout(){
let frame = self.view.bounds
scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
scrollView.isPagingEnabled=true
scrollView.showsHorizontalScrollIndicator=false
scrollView.showsVerticalScrollIndicator=false
scrollView.contentSize = CGSize(width: 4 * frame.size.width, height: frame.size.height)
for i in 1...4 {
let image = UIImage(named: "GuideImage\(Int(i)).png")
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: CGFloat((i-1))*frame.size.width, y: 0, width: frame.size.width, height: frame.size.height)
imageView.contentMode = UIViewContentMode.scaleToFill
scrollView.addSubview(imageView)
}
pageControl = UIPageControl(frame: CGRect(x: frame.size.width/2-100, y: frame.size.height-80, width: 200, height: 100))
startBtn = UIButton(frame: CGRect(x: frame.size.width/2-55, y: frame.size.height-100, width: 110, height: 35))
pageControl.numberOfPages = 4
pageControl.currentPage = 0
startBtn.setTitle("欢迎体验", for: UIControlState.normal)
startBtn.backgroundColor = UIColor.orange
// 给开始按钮设置圆角
startBtn.layer.cornerRadius = 16.0
startBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
//按钮点击动作
startBtn.addTarget(self, action: #selector(GuideViewController.guideOver), for: UIControlEvents.touchUpInside)
startBtn.alpha = 0.0
scrollView.bounces=false
scrollView.delegate=self
self.view.addSubview(scrollView)
self.view.addSubview(pageControl)
self.view.addSubview(startBtn)
}
//引导结束,开始使用App
func guideOver(){
let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
let viewController = mainStoryboard.instantiateInitialViewController()
self.present(viewController!, animated: true, completion:nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}



3、 最后来看引导页的效果图:

GuideView

分享到 评论