12

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 reveal the view beneath it, to be blurred. How would I go about doing this?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

4 Answers4

18

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];
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • 3
    Looks like this only works when the view completely covers the other views that are below it. When it only covers them partially, the entire area below the view is blurred. – adib Jan 31 '13 at 11:59
  • Sorry to bring this back from the dead - what framework(s) does 'setLayer' depend on? – capikaw Feb 17 '13 at 22:56
  • @adib — do you know any workaround for that problem? – Raffael Oct 06 '13 at 20:34
  • 4
    Ah, @adib, found it: set masksToClip to YES and it works :) — and the parent view must be layer-backed, too. – Raffael Oct 07 '13 at 09:55
3

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/

Raffael
  • 1,119
  • 10
  • 20
2

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 :-)

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
-3

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

NMS
  • 25
  • 4