【问题标题】:how to get window with semi-transparent blurred background如何获得具有半透明模糊背景的窗口
【发布时间】:2012-06-25 20:34:50
【问题描述】:

我想要一个具有半透明模糊背景的窗口,就像终端可以做的那样。观看此视频,大约 30 秒,了解我的意思:http://www.youtube.com/watch?v=zo8KPRY6-Mk

在此处查看图片:http://osxdaily.com/wp-content/uploads/2011/04/mac-os-x-lion-terminal.jpg

我已经在谷歌上搜索了一个小时,但找不到任何工作。我相信我需要以某种方式创建一个核心动画层并添加一个背景过滤器,但到目前为止我一直没有成功......我只看到我的窗口的灰色背景。这是我目前得到的代码:

代码:

//  Get the content view -- everything but the titlebar.
NSView *theView = [[self window] contentView];
[theView setAlphaValue:0.5];

// Create core animation layer, with filter
CALayer *backgroundLayer = [CALayer layer];
[theView setWantsLayer:YES];
[theView setLayer:backgroundLayer]; 
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
[theView layer].backgroundFilters = [NSArray arrayWithObject:blurFilter];  
[[theView layer] setBackgroundFilters:[NSArray arrayWithObject:blurFilter]];

有任何提示或示例来做我想做的事吗? 谢谢!

【问题讨论】:

  • 你正在寻找一个叫做HUD窗口的东西!尝试在谷歌上搜索它会帮助你
  • 谢谢,但我仍然没有得到太多。找到了几篇有前途的论坛帖子,但指出了不再存在的示例或博客帖子。
  • 坦率地说,我怀疑没有私有 API 很容易实现模糊。例如。 stackoverflow.com/questions/5901135/…
  • 在最新版本的 OSX/Xcode 中有一个可行的解决方案:stackoverflow.com/questions/41475551/…

标签: macos cocoa calayer nswindow


【解决方案1】:

不需要图层和过滤器,NSWindow可以自己做

[mywindow setOpaque:NO];
[mywindow setBackgroundColor: [NSColor colorWithCalibratedHue:0.0 saturation:0.0 brightness:0.2 alpha:0.5]];

请不要使用它,因为它也会使您的标题栏出现字母(张贴在这里以防其他人需要)

[mywindow setOpaque:NO];
[mywindow setBackgroundColor: [NSColor blackColor]];
[mywindow setAlphaValue:0.5];

【讨论】:

  • 这很奇怪,第一种方法对我不起作用(在 init 中进行)。也在 awakeFromNib 中尝试过,我还应该在哪里执行此操作(我在 nswindow 子类中)
  • 不,您唯一可以控制的是透明度(彩色)。
  • 因此,macOS 终端将允许我们设置模糊强度。你是说除非我们深入研究私有框架领域,否则我们无法做到这一点? Terminal 是如何实现这种效果的?
【解决方案2】:

对于透明度,请使用赵九龙的建议。

对于模糊的背景使用这个

对 NSWindow 的调用:

[self enableBlurForWindow:self];

功能:

-(void)enableBlurForWindow:(NSWindow *)window
{
    //!!!! Uses private API - copied from http://blog.steventroughtonsmith.com/2008/03/using-core-image-filters-onunder.html

    CGSConnection thisConnection;
    uint32_t compositingFilter;
    int compositingType = 1; // Under the window

    /* Make a new connection to CoreGraphics */
    CGSNewConnection(NULL, &thisConnection);

    /* Create a CoreImage filter and set it up */
    CGSNewCIFilterByName(thisConnection, (CFStringRef)@"CIGaussianBlur", &compositingFilter);
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:2.0] forKey:@"inputRadius"];
    CGSSetCIFilterValuesFromDictionary(thisConnection, compositingFilter, (__bridge CFDictionaryRef)options);

    /* Now apply the filter to the window */
    CGSAddWindowFilter(thisConnection, [window windowNumber], compositingFilter, compositingType);
}

注意:它使用私有 API

【讨论】:

【解决方案3】:

对于那些在 2017 年阅读本文并使用 Swift 4 并希望更改您的 BG Alpha 的人,您可以将以下内容添加到您的自定义 NSWindow 类中:

self.backgroundColor = NSColor.black
self.backgroundColor = NSColor.init(calibratedHue: 0, saturation: 0, brightness: 0, alpha: 0.2)

附言我还不需要模糊效果,当我需要时,我会更新答案

【讨论】:

    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多