UIBezierPath 具有透明内部矩形的阴影 Objective c
通过使用UibezierPath添加阴影,我有问题。我的代码是
CGRect f = view.bounds;
view.layer.shadowColor = [UIColor redColor].CGColor;
view.layer.shadowOpacity = 1;
view.layer.shadowRadius = 10;
CGFloat shadowWidth = 5;
CGRect shadowRect = CGRectMake(-shadowWidth, -shadowWidth, f.size.width+(shadowWidth*2), f.size.height+(shadowWidth*2));
CGFloat shadowRadius = radius;
view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:shadowRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(shadowRadius, shadowRadius)].CGPath;
view.layer.shadowOffset = CGSizeMake(0, 0);
我试图使用此代码添加红色阴影。问题是我正在设置视图的透明背景颜色。因此,添加的红色阴影层在背景上变得可见,而不是父背景颜色。如下图像
但我希望它像这样
如果有任何解决方案,请指导。
I have a problem by adding shadow using UIbezierPath. My Code is
CGRect f = view.bounds;
view.layer.shadowColor = [UIColor redColor].CGColor;
view.layer.shadowOpacity = 1;
view.layer.shadowRadius = 10;
CGFloat shadowWidth = 5;
CGRect shadowRect = CGRectMake(-shadowWidth, -shadowWidth, f.size.width+(shadowWidth*2), f.size.height+(shadowWidth*2));
CGFloat shadowRadius = radius;
view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:shadowRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(shadowRadius, shadowRadius)].CGPath;
view.layer.shadowOffset = CGSizeMake(0, 0);
I am trying to add red shadow using this code. The problem is I am setting transparent background color of my view. Due to this the added red shadow layer becomes visible on background, rather the parent background color. like following image
But I want it to be like this
If there is any solution for the problem please guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来实现此目的
CAShapeLayer
作为子图层这是一个简单的示例视图子类和演示它的控制器:
自定义
UIView
子类示例视图控制器
它的外观:
编辑 - 我应该已经意识到 Objective-C 实现的需要(这些天越来越少的请求)。
因此,这里与上面相同,但在 Obj-C(默认标头)中:
自定义
UIView
子类示例视图控制器
You can do this by
CAShapeLayer
as a sublayerHere's a quick example view subclass and a controller demonstrating it:
Custom
UIView
subclassExample view controller
How it looks:
Edit - I should have caught the need for Objective-C implementation (fewer and fewer requests for that theses days).
So, here's the same as above, but in Obj-C (default headers):
Custom
UIView
subclassExample view controller
谢谢@donmag!我从他的班级那里得到了 Shadowpathview 的帮助,以编辑我的目标C函数如下
Thanks @DonMag! I got help from his class ShadowPathView to edit my Objective C function as follows