How to add a blurred view ontop of a view?

后端 未结 4 1244
灰色年华
灰色年华 2021-01-30 09:54

I have an NSTableView that gets reloaded. While new data is loading, I want to add a subview ontop of it with a spinner. I would like the view ontop to be semi-transparent and r

相关标签:
4条回答
  • 2021-01-30 10:11

    The basic technique would be to snap an image of your view, using something like the ‑bitmapImageRepForCachingDisplayInRect: method of NSView, processing that image to make it blurred (Core Image is your friend here) and then overlay your view with an NSImageView containing the blurred image.

    This is fakery, of course, but that's what showmanship is about :-)

    0 讨论(0)
  • 2021-01-30 10:16

    Have you tried changing the Alpha attribute for the view (for transparency)? Also here's a link on blurring a view: Blur Effect for UIView

    0 讨论(0)
  • 2021-01-30 10:23

    You should check out RMBlurredView on guthub: https://github.com/raffael/RMBlurredView

    It's an easy to use subclass of NSView that does all that for you. Be sure to set setWantsLayer:YES on your parent view!

    For details, check out Cocoanetics article: http://www.cocoanetics.com/2013/10/blurring-views-on-mac/

    0 讨论(0)
  • 2021-01-30 10:24

    The easiest solution—significantly more so than the -bitmapImageRepEtc: one, and more applicable to Mac OS than the rasterization-scale method—is to set your overlay view to use a Core Animation backing layer, then give that layer a Core Image blur filter. It's a technique used all over the Mac OS, from the Dock menus to the menu bar itself. Interface Builder makes it trivially easy to set up, but you can do it in code as well, like this:

    CALayer *backgroundLayer = [CALayer layer];
    [backgroundView setLayer:backgroundLayer];
    [backgroundView setWantsLayer:YES];
    
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setDefaults];
    
    [backgroundView layer].backgroundFilters = [NSArray arrayWithObject:blurFilter];
    
    0 讨论(0)
提交回复
热议问题