实现原理:创建一个类(单例),这个类监听App即将离开前台,进入后台,即将进入前台,进入前台的通知,在合适的时机添加一个半透明的毛玻璃View到window上,在进入前台的时候删除这个View
核心代码
notificationCenter.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
添加view
func addView() {
application.ignoreSnapshotOnNextApplicationLaunch()
if let view = view {
window?.addSubview(view)
window?.bringSubviewToFront(view)
view.alpha = 1.0
}
}
移除view
func removeView(completion: (() -> Swift.Void)? = nil) {
guard !force else {
return
}
if let view = view {
guard animation.duration > 0 else {
view.alpha = 0
view.removeFromSuperview()
completion?()
return
}
UIView.animate(withDuration: animation.duration, delay: animation.delay, usingSpringWithDamping: animation.dampingRatio, initialSpringVelocity: animation.velocity, options: animation.options, animations: {
view.alpha = 0
}, completion: { _ in
view.removeFromSuperview()
completion?()
})
}
}
实现类似支付宝,只需要传一个毛玻璃效果的view
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// LaunchScreenSnapshot.protect()
// 实现类似支付宝,只需要传一个毛玻璃效果的view
let blurView = DynamicBlurView(frame: UIScreen.main.bounds)
blurView.blurRadius = 10
LaunchScreenSnapshot.protect(with: blurView, trigger: LaunchScreenSnapshot.Trigger.didEnterBackground, animation: LaunchScreenSnapshot.Animation.init(), force: false)
return true
}
可自定义显示view,如下
demo.gif
网友评论