在 Swift 5 中的 applicationWillResignActive 中显示图像
Show an Image in applicationWillResignActive in Swift 5
所以,当应用程序进入后台时,我试图在最近的屏幕上显示图像。我已经实现了模糊效果,但我想用自定义背景白色的图像替换模糊效果。我不确定,在项目中的什么地方添加图像以及如何使用它。
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
let TAG_BLUR_VIEW = 12345
var blurView:UIVisualEffectView?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationWillResignActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=11=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
if blurView == nil{
blurView = UIVisualEffectView(frame: UIScreen.main.bounds)
blurView?.effect = UIBlurEffect(style: .light)
blurView?.tag = TAG_BLUR_VIEW
}
self.window.rootViewController?.view.insertSubview(blurView!, at: 0)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=11=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
}
}
仅供参考:这是一个 flutter 项目,我将为此功能进行本地化。所以这里总共 swift 个新手。
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
// this tag can be any number. This tag will get assingned to the blurView,
// This tag will be used to removed the blur, when the app is in foreground.
let TAG_BLUR_VIEW = 12345
var blurView:UIImageView?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationWillResignActive(_ application: UIApplication) {
let centerPos = self.window?.rootViewController?.view.center ?? .zero
if let view = self.window.rootViewController?.view.subviews.first(where: {[=10=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
if blurView == nil{
blurView = UIImageView(image: UIImage(named: "secure.jpg")) //replace your image
blurView?.backgroundColor = UIColor .white
blurView?.tag = TAG_BLUR_VIEW
blurView?.center = centerPos
}
// The bulr screen with the tag "12345" is added to the index 0
self.window.rootViewController?.view.insertSubview(blurView!, at: 0)
}
// if the tag on the index 0, matches our blur tag, it removes the blur once the app is on foreground.
override func applicationDidBecomeActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=10=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
}
}
所以,当应用程序进入后台时,我试图在最近的屏幕上显示图像。我已经实现了模糊效果,但我想用自定义背景白色的图像替换模糊效果。我不确定,在项目中的什么地方添加图像以及如何使用它。
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
let TAG_BLUR_VIEW = 12345
var blurView:UIVisualEffectView?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationWillResignActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=11=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
if blurView == nil{
blurView = UIVisualEffectView(frame: UIScreen.main.bounds)
blurView?.effect = UIBlurEffect(style: .light)
blurView?.tag = TAG_BLUR_VIEW
}
self.window.rootViewController?.view.insertSubview(blurView!, at: 0)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=11=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
}
}
仅供参考:这是一个 flutter 项目,我将为此功能进行本地化。所以这里总共 swift 个新手。
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
// this tag can be any number. This tag will get assingned to the blurView,
// This tag will be used to removed the blur, when the app is in foreground.
let TAG_BLUR_VIEW = 12345
var blurView:UIImageView?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationWillResignActive(_ application: UIApplication) {
let centerPos = self.window?.rootViewController?.view.center ?? .zero
if let view = self.window.rootViewController?.view.subviews.first(where: {[=10=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
if blurView == nil{
blurView = UIImageView(image: UIImage(named: "secure.jpg")) //replace your image
blurView?.backgroundColor = UIColor .white
blurView?.tag = TAG_BLUR_VIEW
blurView?.center = centerPos
}
// The bulr screen with the tag "12345" is added to the index 0
self.window.rootViewController?.view.insertSubview(blurView!, at: 0)
}
// if the tag on the index 0, matches our blur tag, it removes the blur once the app is on foreground.
override func applicationDidBecomeActive(_ application: UIApplication) {
if let view = self.window.rootViewController?.view.subviews.first(where: {[=10=].tag == TAG_BLUR_VIEW}){
view.removeFromSuperview()
blurView = nil
}
}
}