【问题标题】:Detect when home button is pressed检测何时按下主页按钮
【发布时间】:2018-04-17 23:02:07
【问题描述】:

在我的应用程序中,当用户按下主页按钮时,我需要隐藏 UIImageView。我尝试使用 pressesBegan(_: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()
}

【问题讨论】:

  • 请提供更多您的代码,以便我们了解发生了什么。
  • 此处缺少完整代码

标签: ios swift


【解决方案1】:

通常,您需要的工具是UIApplicationDelegate.applicationWillResignActive(_:)。这比按下主页按钮时更频繁地调用,但通常这就是您的实际意思。 (在按下主页按钮时不会调用。)

【讨论】:

  • 只有在应用退出活动时才会调用。但在我的情况下,我需要在用户按下主页按钮时立即调用的东西。感谢您的帮助!
  • 当用户按下主页按钮时,iOS 中没有任何内容被专门调用。如果要保护屏幕上的敏感数据,请使用 willResignActive 或 didEnterBackground。请参阅developer.apple.com/library/content/documentation/iPhone/…(作为负责任的后台应用程序,在移至后台之前从视图中删除敏感信息)以获取 Apple 对此问题的指导。
  • “Resign active”发生在“将进入后台”不会发生的少数情况下(例如当有来电时,但随后您又回到应用程序),所以问题是什么你想在这些情况下做。
【解决方案2】:

据我所知,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.
}

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2016-05-31
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多