Adjust NSVisualEffectView blur radius and transparency

前端 未结 2 1663
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题