为什么maskToBounds = YES会阻止CALayer阴影?

jch*_*ard 81 objective-c calayer uiview ios

使用以下代码片段,我将向我的UIView添加阴影效果.哪个效果很好.但是只要我将视图的masksToBounds属性设置为YES即可.投影效果不再呈现.

self.myView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.myView.layer.shadowOpacity = 1.0;
self.myView.layer.shadowRadius = 10.0;
self.myView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.myView.layer.cornerRadius = 5.0;
self.myView.layer.masksToBounds = YES; // <-- This is causing the Drop shadow to not be rendered
UIBezierPath *path = [UIBezierPath bezierPathWithCurvedShadowForRect:self.myView.bounds];
self.myView.layer.shadowPath = path.CGPath;
self.myView.layer.shouldRasterize = YES;
Run Code Online (Sandbox Code Playgroud)

你有什么想法吗?

The*_*uad 164

因为阴影是在视图外部完成的效果,并且maskToBounds设置为YES将告诉UIView不要绘制任何外部的东西.

如果你想要带有阴影的roundedCorner视图,我建议你用2个视图来做:

UIView *view1 = [[UIView alloc] init];
UIView *view2 = [[UIView alloc] init];

view1.layer.cornerRadius = 5.0;
view1.layer.masksToBounds = YES;
view2.layer.cornerRadius = 5.0;
view2.layer.shadowColor = [[UIColor blackColor] CGColor];
view2.layer.shadowOpacity = 1.0;
view2.layer.shadowRadius = 10.0;
view2.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
[view2 addSubview:view1];
[view1 release];
Run Code Online (Sandbox Code Playgroud)

  • 该死的!那讲得通! (9认同)

Phi*_*007 16

现在是iOS 6,事情可能已经改变了.TheSquad的答案对我来说不起作用,直到我设法再添加一行view2.layer.masksToBounds = NO;,否则阴影不显示.虽然文档说masksToBounds默认为NO,但我的代码显示相反.

这是我如何制作带阴影的圆角按钮,这是我应用中最常用的代码段.

button.layer.masksToBounds = YES;
button.layer.cornerRadius = 10.0f;

view.layer.masksToBounds = NO;      // critical to add this line
view.layer.cornerRadius = 10.0f;
view.layer.shadowOpacity = 1.0f;
// set shadow path to prevent horrible performance
view.layer.shadowPath = 
    [UIBezierPath bezierPathWithRoundedRect:_button.bounds cornerRadius:10.0f].CGPath;      

[view addSubview:button];
Run Code Online (Sandbox Code Playgroud)

编辑

如果需要对视图进行动画处理或滚动,masksToBounds = YES则会显着降低税收效果,这意味着动画可能会变得混乱.要获得圆角和阴影以及平滑动画或滚动,请使用以下代码:

button.backgroundColor = [UIColor clearColor];
button.layer.backgroundColor = [UIColor redColor].CGColor;
button.layer.masksToBounds = NO;
button.layer.cornerRadius = 10.0f;

view.layer.shadowOpacity = 0.5f;
view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:_button.bounds cornerRadius:10.0f].CGPath;
view.layer.shadowOffset = CGSizeMake(0.0f, 4.0f);
view.layer.shadowRadius = 2.0f;
view.layer.masksToBounds = NO;
view.layer.cornerRadius = 10.0f;  

[view addSubview:button];
Run Code Online (Sandbox Code Playgroud)

  • +1为第一个答案.您更新的答案不适用于圆角.如果将"masksToBounds"设置为"NO",则圆角将消失.而不是改变你可以使用"shouldRasterize"属性来获得良好的性能. (4认同)

Fan*_*ing 5

带有 StoryBoard 的 Swift 3.0 版本

与@TheSquad 相同的想法。在实际视图下创建一个新视图并将阴影添加到下面的视图中。

1.在实际视图下创建视图

将 aUIView拖到与目标视图具有相同约束的 StoryBoard。检查要绑定到目标视图的剪辑。还要确保新视图列在目标视图之前,以便目标视图覆盖新视图。

在此处输入图片说明

2.现在将新视图链接到您的代码添加阴影

这只是一个示例。你可以在这里做任何你想做的事

shadowView.layer.masksToBounds = false
shadowView.layer.shadowColor = UIColor.red.cgColor
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.shadowOffset = CGSize(width: -1, height: 1)
shadowView.layer.shadowRadius = 3

shadowView.layer.shadowPath = UIBezierPath(rect: coverImage.bounds).cgPath
shadowView.layer.shouldRasterize = true
Run Code Online (Sandbox Code Playgroud)