如何使用NSVisualEffectView将窗口与背景混合 [英] How can I use NSVisualEffectView to blend window with background

查看:219
本文介绍了如何使用NSVisualEffectView将窗口与背景混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于旧版本的Swift/Xcode似乎有很多问题,但是由于某些原因,它没有使用最新的更新.我创建了一个NSVisualEffectView,blurryView,并将子视图添加到我的主视图中:

There seem to be a bunch of questions on this for old versions of Swift/Xcode, but for some reason it hasn't been working with the latest update. I created a NSVisualEffectView, blurryView, and added the subview to my main view:

class ViewController: NSViewController {
    @IBOutlet weak var blurryView: NSVisualEffectView! 
    override func viewDidLoad() {
        super.viewDidLoad()

        //background styling
        blurryView.wantsLayer = true
        blurryView.blendingMode = NSVisualEffectBlendingMode.behindWindow
        blurryView.material = NSVisualEffectMaterial.dark
        blurryView.state = NSVisualEffectState.active

        self.view.addSubview(blurryView, positioned: NSWindowOrderingMode.above, relativeTo: nil)

        // Do any additional setup after loading the view.
    }
...
}

但是,当我运行它时,在窗口上没有任何影响. (当我将其设置在窗口中,并在其他视图之上放置时,模糊可以正常工作,但我只希望窗口模糊.)我还尝试在App Delegate类中做同样的事情,但是我可以不能将我的窗口作为插座连接,因此无法将模糊的视图添加到窗口中.代码如下所示:

But when I run it, there is no effect on the window. (when I set it to within window, and layer it on top of my other view, the blur works correctly, but I only want the window to blur.) I also tried doing the same thing in my App Delegate class, but I can't connect my window as an outlet, and therefore can't add the blurry view to the window. Here's what the code would look like:

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        blurryView.wantsLayer = true
        blurryView.blendingMode = NSVisualEffectBlendingMode.withinWindow
        blurryView.material = NSVisualEffectMaterial.dark
        blurryView.state = NSVisualEffectState.active

        self.window.contentView?.addSubview(blurryView)

    }
...
}

要了解我要寻找的内容: NSVisualEffectView充满活力

To get an idea if what I'm looking for: NSVisualEffectView Vibrancy

推荐答案

它很简单:

  1. 在Interface Builder中,直接拖动NSVisualEffectView作为场景主视图的子视图.
  2. Properties Inspector中将Blending Mode设置为Behind Window
  3. 将所需的其余视图添加为NSVisualEffectView
  4. 的子视图
  5. 就这样,您完成了
  1. In Interface Builder drag a NSVisualEffectView directly as a subview of the main view of your scene.
  2. In the Properties Inspector set Blending Mode to Behind Window
  3. Add the rest of the views you need as subviews of the NSVisualEffectView
  4. That's it, you're done

这是一个例子:

Panel 1 View Controller是我的模糊视图,Background View是我的真实"视图层次结构中的第一个(非模糊)视图.

Panel 1 View Controller is my blurred view, Background View is the first (non-blurred) view in my "real"view hierarchy.

这篇关于如何使用NSVisualEffectView将窗口与背景混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆