Wat*_*hiJ 3 macos ios swift swiftui
当从窗口检测到 keyUp 时,我试图更改状态值。但是,它似乎对我不起作用。这是我的代码
/// Window
class Window: NSWindow {
override func keyUp(with event: NSEvent) {
(contentView as! NSHostingView<ContentView>).rootView.keyPressed(with: event)
}
}
/// ContentView
struct ContentView {
@State var index: Int = 0
var body: some View { ... }
func keyPressed(with event: NSEvent) {
self.index = self.index + 1
}
}
Run Code Online (Sandbox Code Playgroud)
我用调试器测试了一下,显然keyPressed调用成功,但是索引设置不正确。有谁知道为什么?或者在 macOS 的 SwiftUI 中听键盘的正确策略是什么?
这是可能的方法的演示。使用 Xcode 11.4 / macOS 10.15.4 测试
这个想法是通过将发布者注入到环境值来加入自定义 NSWindow,生成关键事件,以及 SwiftUI 视图。这提供了在视图层次结构的任何级别侦听/处理事件的可能性。
下面是完整的模块 (AppDelegate.swift) 代码。另请参阅代码中的有用注释。
import Cocoa
import SwiftUI
import Combine
// Environment key to hold even publisher
struct WindowEventPublisherKey: EnvironmentKey {
static let defaultValue: AnyPublisher<NSEvent, Never> =
Just(NSEvent()).eraseToAnyPublisher() // just default stub
}
// Environment value for keyPublisher access
extension EnvironmentValues {
var keyPublisher: AnyPublisher<NSEvent, Never> {
get { self[WindowEventPublisherKey.self] }
set { self[WindowEventPublisherKey.self] = newValue }
}
}
// Custom window holding publisher and sending events to it. In general
// it can be any event, but for originated question we limit to keyUp events
class Window: NSWindow {
private let publisher = PassthroughSubject<NSEvent, Never>() // private
var keyEventPublisher: AnyPublisher<NSEvent, Never> { // public
publisher.eraseToAnyPublisher()
}
override func keyUp(with event: NSEvent) {
publisher.send(event)
}
}
// Root demo view
struct DemoKeyPressedView: View {
@Environment(\.keyPublisher) var keyPublisher // << access to publisher
@State private var index: Int = 0
var body: some View {
Text("Demo \(index)")
.onReceive(keyPublisher) { event in // << listen to events
self.keyPressed(with: event)
}
}
func keyPressed(with event: NSEvent) {
self.index += 1
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: Window!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the custom window
window = Window(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
// Create the SwiftUI view that provides the window contents.
let contentView = DemoKeyPressedView()
.frame(minWidth: 400, maxWidth: .infinity, maxHeight: .infinity)
.environment(\.keyPublisher, window.keyEventPublisher) // inject publisher
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
626 次 |
| 最近记录: |