從開發(fā)者角度講app啟動(dòng)當(dāng)然越快越好,但是有時(shí)候總有些奇葩需求。比如,讓啟動(dòng)頁故意延時(shí)、在啟動(dòng)頁上加個(gè)動(dòng)畫等等。下面我們來實(shí)現(xiàn)一個(gè)啟動(dòng)頁動(dòng)畫。##
啟動(dòng)頁動(dòng)畫
- 創(chuàng)建一個(gè)啟動(dòng)頁文件
a: 創(chuàng)建iOS工程的時(shí)候默認(rèn)會(huì)有個(gè)LaunchScreen.storyboard文件作為靜態(tài)啟動(dòng)頁,這里我們不用它,我們創(chuàng)建一個(gè)LaunchScreen.xib文件作為靜態(tài)啟動(dòng)頁。這里就不用詳細(xì)說明怎么創(chuàng)建xib了。。。注意一下命名?。?!
b:創(chuàng)建好LaunchScreen.xib之后,在文件中添加一個(gè)UIImageView設(shè)置好你的啟動(dòng)頁圖片。
c:刪除LaunchScreen.storyboard文件
此時(shí)編譯運(yùn)行你就可以看到靜態(tài)啟動(dòng)頁了
- 代碼相關(guān)
思路: 在蘋果粑粑的設(shè)計(jì)中,啟動(dòng)頁應(yīng)該就只是需要一張靜態(tài)圖就ok了,畢竟對用戶使用來說,app啟動(dòng)越快越好。因此沒有常規(guī)的方法實(shí)現(xiàn)啟動(dòng)頁動(dòng)畫。我的想法是在啟動(dòng)后獲取到啟動(dòng)頁的內(nèi)容然后將其填充到一個(gè) UIViewController 中,再構(gòu)建相關(guān)動(dòng)畫。下面說具體步驟
a: 創(chuàng)建 UIViewController 文件
//獲取啟動(dòng)頁文件名
lazy var launchName : String = {
let tempLaunchName = Bundle.main.infoDictionary!["UILaunchStoryboardName"] as! String
return tempLaunchName
}()
//將啟動(dòng)頁內(nèi)容,并生成UIImageView
lazy var launchView : UIImageView = {
let viewCopy = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self.view)) as! UIView
let window = UIApplication.shared.keyWindow
window?.addSubview(viewCopy)
UIGraphicsBeginImageContextWithOptions(UIScreen.main.bounds.size, true, 0.0)
viewCopy.layer.render(in: UIGraphicsGetCurrentContext()!)
viewCopy.removeFromSuperview()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let launchView = UIImageView.init(image: image)
launchView.frame = UIScreen.main.bounds
return launchView
}()
//構(gòu)建view,并將launchView添加進(jìn)去
override func viewDidLoad() {
super.viewDidLoad()
NSKeyedArchiver.archivedData(withRootObject: self.view)
let app : UIApplication = UIApplication.shared;
self.view = UINib.init(nibName: self.launchName, bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView;
self.view.layoutIfNeeded()
app.keyWindow?.addSubview(self.launchView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//TODO 啟動(dòng)頁相關(guān)動(dòng)畫
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.5) {
//動(dòng)畫結(jié)束執(zhí)行相關(guān)跳轉(zhuǎn)邏輯
self.goToNextPage()
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 9.0, *) {
}else{
self.window = UIWindow.init(frame: UIScreen.main.bounds)
}
let launchViewController = XBLaunchViewController.init();
self.window?.rootViewController = launchViewController;
self.window?.makeKeyAndVisible()
return true
}