我们UIViewController可以轻松地将观察者添加到控制器。喜欢:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(didTakeScreenshot(notification:)), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
}
@objc func didTakeScreenshot(notification: Notification) {
print("Screen Shot Taken")
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用以下命令捕获记录:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let isCaptured = UIScreen.main.isCaptured
return true
}
Run Code Online (Sandbox Code Playgroud)
但如何使用 SwiftUI 做到这一点呢?