在我的应用中,我需要在用户按下主页按钮时隐藏UIImageView。我尝试使用pressBegan(_:with :)但是从未打过电话。有人知道如何检测何时按下主页按钮?
@IBOutlet weak var shouldHideView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func hide(){
self.shouldHideView.isHidden = true
}
override fun pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?){
self.hide()
}
答案 0 :(得分:0)
通常,您想要的工具是UIApplicationDelegate.applicationWillResignActive(_:)
。这被称为比按下主页按钮时更频繁,但通常它实际上是你的意思。 (按下主页按钮时,没有呼叫仅。)
答案 1 :(得分:0)
据我所知,iOS SDK上没有主页按钮按下事件。您可以使用以下任何UIApplicationDelegate方法来观察应用程序何时进入后台:
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Tells the delegate that the app is now in the background.
}
答案 2 :(得分:0)
您也可以使用此:
NotificationCenter.default.addObserver(self, selector: #selector(), name: UIApplication.willResignActiveNotification, object: nil)