Adjust NSVisualEffectView blur radius and transparency

前端 未结 2 1664
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 00:20

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 trie

相关标签:
2条回答
  • 2021-01-03 00:55

    I had the same issue as you had and I have solved it with a little trick seems to do the job that I wanted. I hope that it will also help you.

    So in my case, I have added the NSVisualEffectView in storyboards and set its properties as follows:

    and my View hierarchy is as follows:

    All that reduces the blur is in the NSViewController in:

    override func viewWillAppear() {
        super.viewWillAppear()
        //Adds transparency to the app
        view.window?.isOpaque = false
        view.window?.alphaValue = 0.98 //tweak the alphaValue for your desired effect
    }
    

    Going with your example this code should work in addition with the tweak above:

        let blurView = NSVisualEffectView(frame: view.bounds)
        blurView.blendingMode = .behindWindow
        blurView.material = .fullScreenUI
        blurView.state = .active
        view.window?.contentView?.addSubview(blurView)
    

    Swift 5 code For anyone interested here is a link to my repo where I have created a small prank app which uses the code above: GitHub link

    0 讨论(0)
  • 2021-01-03 00:56

    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];
    }
    

    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.

    0 讨论(0)
提交回复
热议问题