国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS引導頁、啟動頁

2019-11-06 09:56:51
字體:
來源:轉載
供稿:網友

前言

這里使用 launchScreen.storyboard 文件創建啟動圖和引導頁。首次打開項目或者更新后第一次打開時展示引導頁,保存有用戶信息時進入首頁,否則進入登錄注冊頁面。

正文

1. 動態啟動圖

LaunchScreen.storyboard 文件上放入一個圖片。并添加約束,鋪滿整個頁面。為 LaunchScreen.storyboard 文件設置 Storyboard ID 。注意到文件的 ClassUIViewController 。 - LunchScreen基本設置

AppDelegate 文件中設置啟動動畫。

extension AppDelegate { /// 動態啟動圖 filePRivate func setupLaunchImage() { let launchSB = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateViewController(withIdentifier: "launchScreen") let launchView = launchSB.view window?.addSubview(launchView!) // 動畫效果 UIView.animate(withDuration: 1.5, animations: { launchView?.alpha = 0.0 launchView?.layer.transform = CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 1.0) }) { (success) in launchView?.removeFromSuperview() } }}

在啟動時調用方法。必須要先設置 window?.makeKeyAndVisible() ,使 window 可用。否則后面使用 window時,始終為 nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window?.makeKeyAndVisible() setupLaunchImage() return true }

查看效果: 動態啟動圖

如果想要在啟動圖上做些什么,譬如添加鏈接、倒計時等,創建 LaunchScreen 文件對應的 controller 代碼文件,然后設計開發。

2. 引導頁

暫時不考慮是否展示引導頁的問題,目前始終展示引導頁。創建引導頁 .storyboard 文件。關聯相關文件。關于 storyboard 文件中 UIScrollView 文件的使用,參考 Storyboard中的UIScrollView使用自動布局,使其能夠滾動 ,或者 史上最簡單的UIScrollView+Autolayout出坑指南。

關于具體的引導頁內部實現參考代碼,這里解釋下 AppDelegate 文件中的引導操作。首先暫時設置引導頁為 rootViewController ,否則動畫過渡期間會展示 Main.storyboard 下的頁面。其次, 延時加載動畫的目的是讓過渡更加自然些。

fileprivate let guideVC = UIStoryboard(name: "GuidePage", bundle: nil).instantiateViewController(withIdentifier: "GuideViewController") as? GuideViewController func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window?.rootViewController = guideVC window?.makeKeyAndVisible() setupLaunchImage() return true }

AppDelegate 擴展代碼:

extension AppDelegate { /// 動態啟動圖 fileprivate func setupLaunchImage() { let launchSB = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateViewController(withIdentifier: "launchScreen") let launchView = launchSB.view window?.addSubview(launchView!) showGuidePage(0.5) // 動畫效果 UIView.animate(withDuration: 1.5, animations: { launchView?.alpha = 0.0 launchView?.layer.transform = CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 1.0) }) { (success) in launchView?.removeFromSuperview() } } /// 展示引導頁 /// /// - Parameter animationAfter: 延時動畫時間 private func showGuidePage(_ animationAfter: TimeInterval) { // 將引導頁設置為主頁面 let guideView = guideVC?.view window?.addSubview(guideView!) guideView?.alpha = 0.5 // 延時加載動畫 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + animationAfter) { UIView.animate(withDuration: 1.0) { guideView?.alpha = 1.0 } } }}

效果: 引導頁

3. 程序首次啟動或者更新后首次啟動才展示引導頁

首先設置一些全局變量:

// 本地保存版本號public let kAppVersion = "appVersion"http:// 本地賬號public let kUserName = "userName"http:// 本地密碼public let kPassWord = "password"http:/// 從 Storyboard 中讀取 ViewController////// - Parameters:/// - name: SB name/// - identifier: VC identifier/// - Returns: need VCpublic func mViewControllerByStoryboard(sb name: String, vc identifier: String) -> UIViewController { return UIStoryboard(name: name, bundle: nil).instantiateViewController(withIdentifier: identifier)}/// 設置 UserDefaults 值的存取public func mUserDefaultsSetValue(_ value: Any, _ key: String) { UserDefaults.standard.set(value, forKey: key)}public func mUserDefaultsObject(_ key: String) -> Any? { return UserDefaults.standard.object(forKey: key)}

判斷是否是第一次啟動或者版本更新:

/// 是否是首次登陸或者版本更新 /// /// - Returns: 判斷是否要展示引導頁 fileprivate func isFirstLaunchOrUpdated() -> Bool { // 獲取應用上次啟動時保存的版本號 let lastVersion = UserDefaults.standard.object(forKey: kAppVersion) as? String let currentVersion = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String if lastVersion == nil || lastVersion != currentVersion { // 未保存版本號或者已更新 UserDefaults.standard.set(currentVersion, forKey: kAppVersion) UserDefaults.standard.synchronize() return true } else { return false } }

上面方法中 Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String 是從 info.plist 文件中讀取版本號。

通過這個方法,設置引導頁的展示。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if isFirstLaunchOrUpdated() { // 首次打開或者更新后打開 window?.rootViewController = guideVC } else { // 已經打開過,然后判斷是否已經登錄過 if mUserDefaultsObject(kUserName) != nil && mUserDefaultsObject(kPassword) != nil { let mainVC = mViewControllerByStoryboard(sb: "Main", vc: "ViewController") window?.rootViewController = mainVC } else { let loginVC = mViewControllerByStoryboard(sb: "LoginAndRegister", vc: "LoginViewController") as! LoginViewController window?.rootViewController = loginVC } } window?.makeKeyAndVisible() setupLaunchImage() return true }

并在展示引導頁的方法中加入判斷:

/// 展示引導頁 /// /// - Parameter animationAfter: 延時動畫時間 private func showGuidePage(_ animationAfter: TimeInterval) { // 判斷是否是首次啟動或者更新后啟動 guard isFirstLaunchOrUpdated() == true else { return } // 將引導頁設置為主頁面 let guideView = guideVC?.view window?.addSubview(guideView!) guideView?.alpha = 0.5 // 延時加載動畫 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + animationAfter) { UIView.animate(withDuration: 1.0) { guideView?.alpha = 1.0 } } }

結束

更多細節參考源碼: Demo 下載


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛川县| 砚山县| 孟州市| 巴东县| 巴中市| 抚远县| 张家口市| 岢岚县| 嵊州市| 克什克腾旗| 远安县| 红河县| 阜宁县| 德钦县| 逊克县| 平舆县| 通辽市| 塔城市| 郑州市| 潼南县| 云林县| 浦江县| 青川县| 上思县| 鹤庆县| 伽师县| 阳新县| 武清区| 娱乐| 来凤县| 虎林市| 三门县| 敦化市| 如东县| 噶尔县| 芜湖市| 河间市| 云林县| 工布江达县| 甘南县| 余江县|