Ajouter une ombre à CAShapeLayer, de sorte que l'intérieur rest transparent

Je veux append un effet de lueur à un path, comme les éléments d'interface luisants bleus (OS X) lorsqu'ils ont un focus.

J'ai utilisé un CAShapeLayer avec un path (rectangular):

self.borderLayer = [CAShapeLayer layer]; CGPathRef path = CGPathCreateWithRect(self.bounds, NULL); [self.borderLayer setPath:path]; CGPathRelease(path); 

À la fin, cela me donne un UIView transparent avec une bordure autour de lui. (Dans mon cas concret, c'est une ligne pointillée avec une animation supplémentaire, mais cela n'a pas d'importance pour cette question particulière)

J'ai joué avec les propriétés d'ombre de CALayer, mais elles vont toujours remplir tout le calque.

 self.borderLayer.shadowPath = self.borderLayer.path; self.borderLayer.shouldRasterize = YES; 

Ce que je veux c'est que seule la ligne environnante UIViews laisse tomber une ombre, de sorte que l'intérieur de l'UIView rest transparent.

J'avais des problèmes similaires en voyant l'ombre à l'intérieur où je voulais au lieu d'une lueur. Je l'ai résolu en utilisant deux CALayers. Un, dans le code, '_bg' pour le fond (dans mon cas noir avec une opacité de 0.55) et une bordure blanche. L'autre couche, dans le code '_shadow', a un fond clair et ajoute l'effet de lueur. _bg est une sous-vue de la couche _shadow. Voici le code pertinent:

 _bg = [CALayer layer]; _shadow = [CALayer layer]; [self.layer insertSublayer:_shadow atIndex:0]; [_shadow addSublayer:_bg]; _bg.frame = self.bounds; _bg.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:.55].CGColor; _bg.cornerRadius=20.0; _bg.borderColor=[UIColor whiteColor].CGColor; _bg.borderWidth=2.0; _shadow.frame=self.bounds; _shadow.masksToBounds=NO; _shadow.backgroundColor = [UIColor clearColor].CGColor; _shadow.cornerRadius=3.0; _shadow.shadowRadius=3.0; _shadow.shadowColor=[UIColor whiteColor].CGColor; _shadow.shadowOpacity=0.6; _shadow.shadowOffset=CGSizeMake(0.0, 0.0); 

Vous pouvez essayer quelque chose comme ceci:

  //background layer of the shadow layer contentViewBackgroundLayer = [CALayer layer]; contentViewBackgroundLayer.frame = contentView.bounds; contentViewBackgroundLayer.backgroundColor = someColor.CGColor; //shadow layer contentViewShadowLayer = [CALayer layer]; [contentViewShadowLayer addSublayer:contentViewBackgroundLayer]; contentViewShadowLayer.backgroundColor = [UIColor clearColor].CGColor; //shadowRadius contentViewShadowLayer.shadowRadius = 10.0; contentViewShadowLayer.shadowColor = [UIColor blackColor].CGColor; contentViewShadowLayer.shadowOpacity = 0.5; contentViewShadowLayer.shadowOffset = CGSizeMake(0.0, 0.0); //Important!!!! add mask to shadowLayer contentViewBackgroundLayer.frame = contentView.bounds; contentViewShadowLayer.frame = contentView.bounds; CGRect rect = CGRectMake(contentViewShadowLayer.bounds.origin.x - 20, contentViewShadowLayer.bounds.origin.y - 20, contentViewShadowLayer.bounds.size.width + 40, contentViewShadowLayer.bounds.size.height + 40); UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect]; [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, contentView.bounds.size.width, contentView.bounds.size.height)]]; //a rectangle which is transparent surrounded by a bigger rectangle CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.fillRule = kCAFillRuleEvenOdd; shapeLayer.path = [path CGPath]; contentViewShadowLayer.mask = shapeLayer; [contentView.layer insertSublayer:contentViewShadowLayer atIndex:0];