我正在尝试实现窗口切换(我在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)
}
}
旧问题,但是我遇到了同样的问题。在Swift中,使用occlusionState
二进制运算符检查AND
的方式有所不同:
if (window.occlusionState & NSWindowOcclusionState.Visible != nil) {
// visible
}
else {
// not visible
}
[在最近的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.
}
}