objective c - NSView with masked CIFilter for OS X app -


i developing app contains lots of custom nsview objects being moved around. have implemented gaussian blur background filter 1 of custom nsview subclasses so:

- (id)init {     self = [super init];     if (self) {          ...          cifilter *saturationfilter = [cifilter filterwithname:@"cicolorcontrols"];         [saturationfilter setdefaults];         [saturationfilter setvalue:@.5 forkey:@"inputsaturation"];          cifilter *blurfilter = [cifilter filterwithname:@"cigaussianblur"];         [blurfilter setdefaults];         [blurfilter setvalue:@2.0 forkey:@"inputradius"];          self.wantslayer = yes;         self.layer.backgroundcolor = [nscolor clearcolor].cgcolor;         self.layer.maskstobounds = yes;         self.layer.needsdisplayonboundschange = yes;         self.layerusescoreimagefilters = yes;          [self updateframe]; //this frame size set          self.layer.backgroundfilters = @[saturationfilter, blurfilter];          ...          return self;     }     else return nil; } 

this works great , creates gaussian blur effect within entire contents of view. problem not want gaussian blur cover entire view. there (intentional) 12px padding between actual size of nsview , drawing of content box:

- (void)drawrect:(nsrect)dirtyrect {     [super drawrect:dirtyrect];      nscolor* strokecolor = [nscolor colorwithred:.5 green:.8 blue:1 alpha:1];     nscolor* fillcolor = [nscolor colorwithred:.5 green:.8 blue:1 alpha:.2];      ...      [strokecolor setstroke];     [fillcolor setfill];     nsbezierpath *box = [nsbezierpath bezierpathwithroundedrect:nsmakerect(self.bounds.origin.x + 12, self.bounds.origin.y + 12, self.bounds.size.width - 24, self.bounds.size.height - 24) xradius:6 yradius:6];     box.linewidth = 6;     [box stroke];     [box fill];      ... } 

the reason padding there pieces of gui inhabit region , drawn seamlessly containing box. mask blur effect have effect on interior of drawn box rather entire view. here have tried.

attempt 1: create sublayer

i created sublayer in nsview appropriately sized frame, , added blur effect sublayer. problem: blur effect seems apply immediate parent layer, rather blur contents behind nsview, blurs contents of nsview's self.layer (which empty).

attempt 2: create masking layer

i tried create masking layer , set self.layer.mask. however, since positions of gui content change (via drawrect function), need copy of current layer use masking layer. tried following code, had no effect.

self.layer.mask = nil; nsarray *bgfilters = self.layer.backgroundfilters; self.layer.backgroundfilters = nil; calayer *maskinglayer = self.layer.presentationlayer; self.layer.mask = maskinglayer; self.layer.backgroundfilters = bgfilters; 

attempt 3: draw masking layer directly

i not find examples of how draw directly on layer. can not use static uiimage mast with, because, said above, mask has change user interaction. looking equivalent drawrect function. appreciated.

so...

it seems me sublayer way best , simplest way go, if figure out how change priority of blur effect background behind nsview not nsview's background layer behind sublayer.

well, still know if there more elegant way, have found solution works. basically, have created masking layer nsimage drawn modified version of drawrect function:

- (id)init {     self = [super init];     if (self) {          // setup view same above           calayer *masklayer = [calayer layer];         masklayer.contents = [nsimage imagewithsize:self.frame.size flipped:yes drawinghandler:^bool(nsrect dstrect) {             [self drawmask:self.bounds];             return yes;         }];         masklayer.frame = self.bounds;         self.layer.mask = masklayer;          return self;     }     else return nil; }   - (void)drawmask:(nsrect)dirtyrect {     [[nscolor clearcolor] set]; nsrectfill(self.bounds);     [[nscolor blackcolor] set];       // same drawing code drawrect      // except solid black (no alpha transparency)      // , need draw parts effect external boundaries } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -