从开发者角度讲app启动当然越快越好,但是有时候总有些奇葩需求。比如,让启动页故意延时、在启动页上加个动画等等。下面我们来实现一个启动页动画。##
启动页动画
- 创建一个启动页文件
a: 创建iOS工程的时候默认会有个LaunchScreen.storyboard文件作为静态启动页,这里我们不用它,我们创建一个LaunchScreen.xib文件作为静态启动页。这里就不用详细说明怎么创建xib了。。。注意一下命名!!!
b:创建好LaunchScreen.xib之后,在文件中添加一个UIImageView设置好你的启动页图片。
c:删除LaunchScreen.storyboard文件
此时编译运行你就可以看到静态启动页了
- 代码相关
思路: 在苹果粑粑的设计中,启动页应该就只是需要一张静态图就ok了,毕竟对用户使用来说,app启动越快越好。因此没有常规的方法实现启动页动画。我的想法是在启动后获取到启动页的内容然后将其填充到一个 UIViewController 中,再构建相关动画。下面说具体步骤
a: 创建 UIViewController 文件
//获取启动页文件名
lazy var launchName : String = {
let tempLaunchName = Bundle.main.infoDictionary!["UILaunchStoryboardName"] as! String
return tempLaunchName
}()
//将启动页内容,并生成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
}()
//构建view,并将launchView添加进去
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 启动页相关动画
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.5) {
//动画结束执行相关跳转逻辑
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
}
网友评论