显示和隐藏,而不是在可可应用程序在点击关闭终止应用程序窗口

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

我一直在试图显示在接近(红色按钮)隐藏窗口,点击窗口上。我想这样做是隐藏窗口,当用户点击我的应用程序一次它会再次出现。

在此先感谢所有谁提供答案的开发者。我是新来的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
    }
}

我想创建它会在后台运行,如果在接近用户点击这反而掩盖终止的计时器应用。当他从停靠栏菜单中再次点击,就会重新打开该窗口。

swift xcode macos cocoa
2个回答
6
投票

我没有太多的在Mac OS的发展,但我认为你应该从这样的NSWindowController继承:

class MyWindowController: NSWindowController, NSWindowDelegate {

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
}

然后你只需要打开你的主(或任何名称你有)故事板,选择Window Controller和你MyWindowController设置它:

enter image description here

我试着和它的工作对我来说。


-1
投票

我已经找到了解决方案。由于@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正常工作。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.