Iphone 将阴影添加到CAShapeLayer,以便内部保持透明
我想给路径添加一个光晕效果,比如有焦点的(OSX)界面元素周围的蓝色光晕 我使用了一个带有(矩形)路径的CAShapeLayer: 最后,这给了我一个透明的UIView,周围有一个边框。(在我的具体案例中,它是一条带有附加动画的虚线,但这对于这个特定问题并不重要) 我使用了CALayer的阴影属性,但它们将始终填充整个层Iphone 将阴影添加到CAShapeLayer,以便内部保持透明,iphone,ios,objective-c,core-animation,Iphone,Ios,Objective C,Core Animation,我想给路径添加一个光晕效果,比如有焦点的(OSX)界面元素周围的蓝色光晕 我使用了一个带有(矩形)路径的CAShapeLayer: 最后,这给了我一个透明的UIView,周围有一个边框。(在我的具体案例中,它是一条带有附加动画的虚线,但这对于这个特定问题并不重要) 我使用了CALayer的阴影属性,但它们将始终填充整个层 self.borderLayer.shadowPath = self.borderLayer.path; self.borderLayer.shouldRasterize =
self.borderLayer.shadowPath = self.borderLayer.path;
self.borderLayer.shouldRasterize = YES;
我想要的是,只有UIView周围的线条会投射阴影,这样UIView的内部仍然是透明的。我在看到我想要的阴影而不是辉光的内部时遇到了类似的问题。我用两个Calayer解决了这个问题。一个,在代码中,“_bg”作为背景(在我的例子中是黑色,不透明度为0.55)和白色边框。代码“_shadow”中的另一层具有清晰的背景并添加了辉光效果_bg是阴影层的子视图。以下是相关代码:
_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);
您可以尝试以下方法:
//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];
//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];