调整NSVisualEffectView模糊半径和透明度 [英] Adjust NSVisualEffectView blur radius and transparency

查看:354
本文介绍了调整NSVisualEffectView模糊半径和透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将NSVisualEffectView应用于NSWindow(Swift或Objective-C)时,是否可以调整其模糊半径和透明度?我尝试了NSVisualEffectMaterial的所有变体(深色,中等,浅色)-但这没有用.在下图中,我使用了Apple的非公开API,左侧为CGSSetWindowBackgroundBlurRadius,右侧为NSVisualEffectView.

Is it possible to adjust the blur radius and transparency of an NSVisualEffectView when it's applied to an NSWindow (Swift or Objective-C)? I tried all variations of NSVisualEffectMaterial (dark, medium, light) - but that's not cutting it. In the image below I've used Apple's non-public API with CGSSetWindowBackgroundBlurRadius on the left, and NSVisualEffectView on the right.

我试图获得左侧显示的外观,但似乎我被降级为使用右侧的方法.

I'm trying to achieve the look of what's on the left, but it seems I'm relegated to use the methods of the right.

这是我的代码:

blurView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
blurView.material = NSVisualEffectMaterial.Medium
blurView.state = NSVisualEffectState.Active
self.window!.contentView!.addSubview(blurView)

可能相关-但不能回答我的问题:

Possibly, related - but doesn't answer my question:

  • OS X NSVisualEffect decrease blur radius? - no answer

推荐答案

尽管除非您准备好在以后的版本中不使用它,否则我不建议这样做,但是您可以将NSVisualEffectView子类化为以下内容:做你想做的:

Although I wouldn't recommend this unless you are ready to fall back to it not working in a future release, you can subclass NSVisualEffectView with the following to do what you want:

- (void)updateLayer
{
    [super updateLayer];

    [CATransaction begin];
    [CATransaction setDisableActions:YES];

    CALayer *backdropLayer = self.layer.sublayers.firstObject;

    if ([backdropLayer.name hasPrefix:@"kCUIVariantMac"]) {
        for (CALayer *activeLayer in backdropLayer.sublayers) {
            if ([activeLayer.name isEqualToString:@"Active"]) {
                for (CALayer *sublayer in activeLayer.sublayers) {
                    if ([sublayer.name isEqualToString:@"Backdrop"]) {
                        for (id filter in sublayer.filters) {
                            if ([filter respondsToSelector:@selector(name)] && [[filter name] isEqualToString:@"blur"]) {
                                if ([filter respondsToSelector:@selector(setValue:forKey:)]) {
                                    [filter setValue:@5 forKey:@"inputRadius"];
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    [CATransaction commit];
}

尽管这本身并没有使用私有API,但是它确实开始挖掘您不拥有的层层次结构,因此请确保再次检查返回的内容是否符合您的期望,如果没有,请进行适当的失败.例如,在10.10优胜美地,Backdrop层是Visual Effect视图的直接依赖者,因此将来可能会发生变化.

Although this doesn't use Private APIs per se, it does start to dig into layer hierarchies which you do not own, so be sure to double check that what you are getting back is what you expect, and fail gracefully if not. For instance, on 10.10 Yosemite, the Backdrop layer was a direct decedent of the Visual Effect view, so things are likely to change in the future.

这篇关于调整NSVisualEffectView模糊半径和透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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