Swift应用程序,进入后台,和进入前台的高斯模糊效果

时间:2023-02-08 21:26:09

偶然的机会碰到了UIVisualEffectView(菜鸟一只,不要嘲笑),发现原来苹果就是靠这个实现高斯模糊的.
在appdelegate里面先声明一个全局的常量,
然后当应用程序辞去激活的时候,添加一个高斯模糊的试图到应用程序上,
当应用程序已经被激活的时候,在移除这个高斯模糊的试图,
代码如下:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let visualEffect = UIVisualEffectView.init(effect: UIBlurEffect.init(style: .extraLight)) func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) window?.makeKeyAndVisible() window?.rootViewController = JCTTabBarController() return true } //MARK -- 程序辞去激活时调用的方法 func applicationWillResignActive(_ application: UIApplication) { visualEffect.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height) visualEffect.alpha = 0.7 application.keyWindow?.addSubview(visualEffect) } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } //MARK -- 程序已经被激活时调用的方法 func applicationDidBecomeActive(_ application: UIApplication) { visualEffect.removeFromSuperview() } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }