ios – 仅将CALayer阴影屏蔽到rect之外

ios – 仅将CALayer阴影屏蔽到rect之外,第1张

概述我如何用阴影掩盖CALayer,以便阴影只在路径之外?我不希望透明视图背后的阴影. shadowLayer.shadowOffset = CGSizeMake(x, y);shadowLayer.shadowRadius = radius;shadowLayer.shadowOpacity = opacity;shadowLayer.shadowColor = color.CGColor; 我如何用阴影掩盖CALayer,以便阴影只在路径之外?我不希望透明视图背后的阴影.
shadowLayer.shadowOffset = CGSizeMake(x,y);shadowLayer.shadowRadius = radius;shadowLayer.shadowOpacity = opacity;shadowLayer.shadowcolor = color.CGcolor;shadowLayer.shadowPath = [UIBezIErPath bezIErPathWithRect:vIEw.bounds].CGPath;

谢谢.

解决方法@H_502_9@ 我会回答我自己的问题.为视图添加新的阴影层.如果设置正确,这应适用于任何shadowPath.
float radius = 8;float opacity = 0.5f;float x = 4;float y = 6;UIcolor *color = [UIcolor blackcolor];// Shadow layerCALayer *shadowLayer = [CALayer layer];shadowLayer.shadowOffset = CGSizeMake(x,y);shadowLayer.shadowRadius = radius;shadowLayer.shadowOpacity = opacity;shadowLayer.shadowcolor = color.CGcolor;shadowLayer.shadowPath = [UIBezIErPath bezIErPathWithRect:vIEw.frame].CGPath; // Or any other path// Shadow mask frameCGRect frame = CGRectInset(vIEw.layer.frame,-2*radius,-2*radius);frame = CGRectOffset(frame,x,y);// Translate shadowLayer shadow path to mask layer's coordinate systemCGAffinetransform trans = CGAffinetransformMakeTranslation(-vIEw.frame.origin.x-x+2*radius,-vIEw.frame.origin.y-y+2*radius);// Mask pathCGMutablePathref path = CGPathCreateMutable();CGPathAddRect(path,nil,(CGRect){.origin={0,0},.size=frame.size});CGPathAddpath(path,&trans,shadowLayer.shadowPath);CGPathCloseSubpath(path);// Mask layerCAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];maskLayer.frame = frame;maskLayer.fillRule = kCAFillRuleEvenOdd;maskLayer.path = path;shadowLayer.mask = maskLayer;[vIEw.layer.superlayer insertSublayer:shadowLayer below:vIEw.layer];
总结

以上是内存溢出为你收集整理的ios – 仅将CALayer阴影屏蔽到rect之外全部内容,希望文章能够帮你解决ios – 仅将CALayer阴影屏蔽到rect之外所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106678.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存