问题描述
在约塞米蒂,侧边栏具有半透明的活力背景。如何在10.10 / Xcode 6中创建类似的视图?
In Yosemite sidebars have a semitransparent "vibrant" background. How can I create a view like that in 10.10/Xcode 6?
我可以给任何视图这样的背景吗?我发现 NSOutlineView
默认为这样的背景,当你给它源列表高亮样式时,但Calendar.app中的侧边栏似乎不是NSOutlineView,所以我想知道是否有一个通用的解决方案。
Can I give any view such background? I've found that NSOutlineView
will default to such background when you give it "Source list" highlight style, but the sidebar in the Calendar.app doesn't appear to be an NSOutlineView, so I wonder if there's a generic solution for this.
推荐答案
随着Yosemite版本的OSX操作系统的推出,Apple推出了一款新的模式称为 vibrancy ,这是一种光扩散模糊,可以用于Cocoa窗口和窗口组件。这有点像透过淋浴门看,并使用 NSVisualEffectView
。 Apple 。
With the introduction of the Yosemite version of the OSX operating system, Apple introduced a new mode called vibrancy, which is a light diffusion blur, to Cocoa window and window components. It's sort of like looking through a shower door, and uses the NSVisualEffectView
. Apple explains this effect here.
我在NSView上使用此类别。
只需拨打您想要充满活力的视图。
它也与优胜美地前期兼容。 (如果你有一个Yosemite之前,你将看不到效果)
I use this category on NSView.Simply call on the view you want to make vibrant.It is also backwards compatible with pre-Yosemite. (If you have a pre-Yosemite, you won't see the effect)
@implementation NSView (HS)
-(instancetype)insertVibrancyViewBlendingMode:(NSVisualEffectBlendingMode)mode
{
Class vibrantClass=NSClassFromString(@"NSVisualEffectView");
if (vibrantClass)
{
NSVisualEffectView *vibrant=[[vibrantClass alloc] initWithFrame:self.bounds];
[vibrant setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
// uncomment for dark mode instead of light mode
// [vibrant setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
[vibrant setBlendingMode:mode];
[self addSubview:vibrant positioned:NSWindowBelow relativeTo:nil];
return vibrant;
}
return nil;
}
@end
来自@Volomike的详细说明关注...
Detailed instructions from @Volomike follow…
-
添加 AppKit。框架到您的项目设置>构建阶段> 链接二进制文件库,以便它可以识别NSVisualEffectView。
Add AppKit.framework to your Project Settings > Build Phases > Link Binary with Libraries so that it can recognize NSVisualEffectView.
制作主窗口的默认视图的出口代理,而不是窗口本身, AppDelegate.m或AppDelegate.mm文件。 (如果您是新手,请。)出于此目的,我们假设您将其命名为 mainview
,然后可在代码中将其命名为 _mainview
。
Make an outlet delegate of your main window's default view, not the window itself, to your AppDelegate.m or AppDelegate.mm file. (If you're new at that, read this tutorial.) For purposes here, let's assume you named this as mainview
, which then is addressable in code as _mainview
.
在项目中包含类别。如果您是新手,请在 AppDelegate.m或AppDelegate.mm文件中的任何 @implementation
行之前添加类别。
Include the category in your project. If you're new at that, add the category before any @implementation
line in your AppDelegate.m or AppDelegate.mm file.
在 AppDelegate.m或AppDelegate.mm文件中,在 @implementation AppDelegate
,在 applicationDidFinishLaunching
类方法中,添加以下代码行:
In your AppDelegate.m or AppDelegate.mm file, in your @implementation AppDelegate
, inside your applicationDidFinishLaunching
class method, add this line of code:
[_mainview insertVibrancyViewBlendingMode:NSVisualEffectBlendingModeBehindWindow];
- 现在你需要学习如何添加一些代码以在窗口上提供其他元素,以及窗口本身半透明。半透明性将允许此效果根据您的需要显示在窗口组件中。这是。
- Now you need to learn how to add some code to give the other elements on your window, as well as the window itself, translucency. That translucency will allow this effect to show through into your window components as you require. This is explained here.
现在的净效果是,标题栏下方的整个窗口,或者只有你想要的部分(如侧边栏),都会显示出这种活力效果。
The net effect now is that either the entire window below the titlebar, or only parts you want (such as a sidebar), will show this vibrancy effect.
这篇关于如何创建具有半透明/模糊背景的优胜美地风格的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!