In general, you cannot easily set a border around a mask. This is similar to passing a border around the transparent pixels of an image. Perhaps this can be done using image filters. In a more specific case, if you are using a simple CAShapeLayer, here is a sample code that does this:
[CATransaction begin]; [CATransaction setDisableActions:YES]; CALayer *hostLayer = [CALayer layer]; hostLayer.backgroundColor = [NSColor blackColor].CGColor; hostLayer.speed = 0.0; hostLayer.timeOffset = 0.0; CALayer *maskedLayer = [CALayer layer]; maskedLayer.backgroundColor = [NSColor redColor].CGColor; maskedLayer.position = CGPointMake(200, 200); maskedLayer.bounds = CGRectMake(0, 0, 200, 200); CAShapeLayer *mask = [CAShapeLayer layer]; mask.fillColor = [NSColor whiteColor].CGColor; mask.position = CGPointMake(100, 100); mask.bounds = CGRectMake(0, 0, 200, 200); CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 100, 100); for (int i=0; i<20; i++) { double x = arc4random_uniform(2000) / 10.0; double y = arc4random_uniform(2000) / 10.0; CGPathAddLineToPoint(path, NULL, x, y); } CGPathCloseSubpath(path); mask.path = path; CGPathRelease(path); maskedLayer.mask = mask; CAShapeLayer *maskCopy = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:mask]]; maskCopy.fillColor = NULL; maskCopy.strokeColor = [NSColor yellowColor].CGColor; maskCopy.lineWidth = 4; maskCopy.position = maskedLayer.position;
It basically creates an arbitrary path and sets it as a mask. He then takes a copy of this layer to stroke the path. You may need to tweak everything to get the exact effect you're looking for.
aLevelOfIndirection
source share