snap.jpg
demo.jpg
實現原理:創建一個類(單例),這個類監聽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