我一直在试图显示在接近(红色按钮)隐藏窗口,点击窗口上。我想这样做是隐藏窗口,当用户点击我的应用程序一次它会再次出现。
在此先感谢所有谁提供答案的开发者。我是新来的Cocoa程序。我是iOS开发所以我对可可的应用程序没有太大的知识。
我试图隐藏(:)方法和orderOut(:)方法了。但不工作。
class ViewController : NSViewController, NSWindowDelegates {
override func viewDidAppear() {
self.view.window?.delegate = self
}
func windowShouldClose(_ sender: NSWindow) -> Bool {
//NSApplication.shared.terminate(self)
//NSApp.hide(self)
//self.view.window?.orderOut(sender)
return false
}
}
我想创建它会在后台运行,如果在接近用户点击这反而掩盖终止的计时器应用。当他从停靠栏菜单中再次点击,就会重新打开该窗口。
我已经找到了解决方案。由于@Silvester建议提出NSViewController。在按钮单击事件:
@IBAction func onButtonClick(_ sender: VSButton) {
let animator = ReplacePresentationAnimator()
let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
present(vc, animator: animator)
}
自定义动画类:
class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {
func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = fromViewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
fromViewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
viewController.view.alphaValue = 0
window.contentViewController = viewController
viewController.view.animator().alphaValue = 1.0
})
}
}
func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = viewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
viewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
fromViewController.view.alphaValue = 0
window.contentViewController = fromViewController
fromViewController.view.animator().alphaValue = 1.0
})
}
}
}
这将西尔维斯特WindowController正常工作。谢谢。