如何使用NSWindowOcclusionState.Visible在Swift中可见

问题描述 投票:3回答:2

我正在尝试实现窗口切换(我在Objective-C中做了很多次),但是现在在Swift中。似乎我正在错误地使用NSWindowOcclusionState.Visible,但我确实看不到我的问题。初始窗口创建后,仅调用w.makeKeyAndOrderFront(self)行。

有什么建议吗?

var fileArchiveListWindow: NSWindow? = nil

@IBAction func tougleFileArchiveList(sender: NSMenuItem) {
    if let w = fileArchiveListWindow {
        if w.occlusionState == NSWindowOcclusionState.Visible {
            w.orderOut(self)
        }
        else {
            w.makeKeyAndOrderFront(self)
        }
    }
    else {
        let sb = NSStoryboard(name: "FileArchiveOverview",bundle: nil)
        let controller: FileArchiveOverviewWindowController = sb?.instantiateControllerWithIdentifier("FileArchiveOverviewController") as FileArchiveOverviewWindowController
        fileArchiveListWindow = controller.window
        fileArchiveListWindow?.makeKeyAndOrderFront(self)
    }
}
macos swift appkit
2个回答
1
投票

旧问题,但是我遇到了同样的问题。在Swift中,使用occlusionState二进制运算符检查AND的方式有所不同:

if (window.occlusionState & NSWindowOcclusionState.Visible != nil) {
    // visible
}
else {
    // not visible
}

0
投票

[在最近的SDK中,NSWindowOcclusionState位掩码作为OptionSet导入到Swift中。您可以使用window.occlusionState.contains(.visible)检查窗口是否可见(完全遮挡)。

示例:

observerToken = NotificationCenter.default.addObserver(forName: NSWindow.didChangeOcclusionStateNotification, object: window, queue: nil) { note in
    let window = note.object as! NSWindow
    if window.occlusionState.contains(.visible) {
        // window at least partially visible, resume power-hungry calculations
    } else {
        // window completely occluded, throttle down timers, CPU, etc.
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.