按下主页按钮时检测

时间:2018-04-17 23:02:07

标签: ios swift

在我的应用中,我需要在用户按下主页按钮时隐藏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()
}

3 个答案:

答案 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)