在AppDelegate中定义一些必要的属性
/// 本次启动是否要显示引导页,如果显示引导页则不显示启动动画
var isShowingGuideVC = false
/// 启动画面显示后,用来定制动画的ImageView,懒加载
lazy var launchIV:UIImageView! = UIImageView.init(frame: UIScreen.main.bounds)
在func application(application: didFinishLaunchingWithOptions) 方法中添加判断是否需要显示启动画面动画的逻辑
// 在这个方法return的时候执行
defer{
// 不显示引导页
if !isShowingGuideVC{
// 必须使window可见,否则定制无效
window?.makeKeyAndVisible()
/// 加载启动图
self.showLaunchImage()
}
}
加载启动图片,用于衔接启动时系统显示的启动图,在启动图的基础上做动画
func loadLaunchImage() -> UIImage?{
let screenSize = UIScreen.main.bounds.size
let screenOrientation = "Portrait"
guard let infoPlist = Bundle.main.infoDictionary else{
return nil
}
guard let launchImages = infoPlist["UILaunchImages"] as? [[String:String]] else{
return nil
}
for dict in launchImages{
guard let sizeString = dict["UILaunchImageSize"] else{
continue
}
guard let imageOrientation = dict["UILaunchImageOrientation"] else{
continue
}
let imageSize = CGSizeFromString(sizeString)
if imageSize != screenSize || imageOrientation != screenOrientation{
continue
}
guard let imageName = dict["UILaunchImageName"] else{
continue
}
return UIImage.init(named: imageName)
}
return nil
}
将启动图显示到屏幕上,并显示动画
// 延迟执行函数
func delay(seconds:Double,block:@escaping (() -> Void)){
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: {
block()
})
}
/// 加载启动图
func loadLaunchImage(){
guard let image = self.loadLaunchImage() else{
return
}
self.launchIV.image = image
self.launchIV.frame = UIScreen.main.bounds
launchIV.layer.setupCommonShadow()
// AppWindow -> (UIApplication.shared.delegate! as! AppDelegate).window
AppWindow.addSubview(launchIV)
delay(second: 0.3) {
self.showLaunchAnimation()
}
}
/// 显示启动动画
func showLaunchAnimation(){
/// 动画效果:向屏幕左边移动整个画面
let xOffset = -UIScreen.main.bounds.width * 1.5
UIView.animate(withDuration: 1.6, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.launchIV.layer.transform = CATransform3DMakeTranslation(xOffset, 1, 1)
}) { (finish) in
self.launchIV.removeFromSuperview()
self.launchIV = nil
}
}
网友评论