[英]Show and hide window instead of terminating app on close click in cocoa app
我一直试图在关闭(红色按钮)上单击显示隐藏窗口。 我想做的是隐藏窗口,当用户再次单击我的应用程序时,它将再次显示。
在此先感谢所有提供答案的开发人员。 我是可可应用程序的新手。 我是iOS开发人员,所以我对可可应用程序了解不多。
我也尝试了hide( :)方法和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
})
}
}
}
这将与Silvester MyWindowController完美配合。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.