我们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 做到这一点呢?
paw*_*222 10
这是一个简单的演示:
struct ContentView: View {
@State var isRecordingScreen = false
var body: some View {
Text("Test")
.onReceive(NotificationCenter.default.publisher(for: UIApplication.userDidTakeScreenshotNotification)) { _ in
print("Screenshot taken")
}
.onReceive(NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification)) { _ in
isRecordingScreen.toggle()
print(isRecordingScreen ? "Started recording screen" : "Stopped recording screen")
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2756 次 |
| 最近记录: |