Ios 使用SwiftUI时,在拆分视图上接收应用程序之间焦点更改的通知
在iPaOS拆分视图中显示的应用程序或场景的焦点更改视图中,我应该观察什么来接收通知 当用户将焦点返回到应用程序时,我正在尝试更新视图的一些数据,如前所述Ios 使用SwiftUI时,在拆分视图上接收应用程序之间焦点更改的通知,ios,swiftui,ipados,Ios,Swiftui,Ipados,在iPaOS拆分视图中显示的应用程序或场景的焦点更改视图中,我应该观察什么来接收通知 当用户将焦点返回到应用程序时,我正在尝试更新视图的一些数据,如前所述 谢谢。这里有一个解决方案,每当收到UIPasteboard.changedNotification或更改scenePhase时,都会更新pasteDisabled: struct ContentView: View { @Environment(\.scenePhase) private var scenePhase @Sta
谢谢。这里有一个解决方案,每当收到
UIPasteboard.changedNotification
或更改scenePhase
时,都会更新pasteDisabled
:
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
@State private var pasteDisabled = false
var body: some View {
Text("Some Text")
.contextMenu {
Button(action: {}) {
Text("Paste")
Image(systemName: "doc.on.clipboard")
}
.disabled(pasteDisabled)
}
.onReceive(NotificationCenter.default.publisher(for: UIPasteboard.changedNotification)) { _ in
updatePasteDisabled()
}
.onChange(of: scenePhase) { _ in
updatePasteDisabled()
}
}
func updatePasteDisabled() {
pasteDisabled = !UIPasteboard.general.contains(pasteboardTypes: [aPAsteBoardType])
}
}
当应用程序进入后台时,场景库似乎会发生变化,但当应用程序处于分割视图时,当应用程序之间的焦点发生变化时,场景库不会发生变化。没有这一点,我无法知道应用程序是否有很多焦点。