使用以下代码片段,我将向我的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;
你对此有什么想法吗?
答案 0 :(得分:163)
因为阴影是在视图外部完成的效果,并且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];
答案 1 :(得分:16)
现在是iOS 6,事情可能已经改变了。在我设法再添加一行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];
修改强>
如果需要动画或滚动视图,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];
答案 2 :(得分:4)
这是@TheSquad发布的答案的Swift 3和IBDesignable版本。
我在故事板文件中进行更改时使用了相同的概念。首先,我将 targetView (需要角半径和阴影的那个)移动到新的 containerView 中。然后我添加了以下代码行(参考:https://stackoverflow.com/a/35372901/419192)来为UIView类添加一些IBDesignable属性:
@IBDesignable extension UIView {
/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */
@IBInspectable var shadowColor: UIColor? {
set {
layer.shadowColor = newValue!.cgColor
}
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
else {
return nil
}
}
}
/* The opacity of the shadow. Defaults to 0. Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */
@IBInspectable var shadowOpacity: Float {
set {
layer.shadowOpacity = newValue
}
get {
return layer.shadowOpacity
}
}
/* The shadow offset. Defaults to (0, -3). Animatable. */
@IBInspectable var shadowOffset: CGPoint {
set {
layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
}
get {
return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
}
}
/* The blur radius used to create the shadow. Defaults to 3. Animatable. */
@IBInspectable var shadowRadius: CGFloat {
set {
layer.shadowRadius = newValue
}
get {
return layer.shadowRadius
}
}
/* The corner radius of the view. */
@IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
}
get {
return layer.cornerRadius
}
}
添加此代码后,我回到故事板并选择 containerView ,现在我可以在属性检查器中找到一组新属性:
除了根据我的选择添加这些属性的值之外,我还在 targetView 中添加了一个角半径,并将masksToBounds属性设置为true。
我希望这会有所帮助:)
答案 3 :(得分:3)
与@TheSquad相同的想法。在实际视图下创建一个新视图,并在下视图中添加阴影。
<强> 1。在实际视图下创建视图
将UIView
拖动到与目标视图具有相同约束的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
答案 4 :(得分:2)
我的阴影和圆角也出现了严重的性能问题。我没有使用shadowPath部分,而是使用以下几行来完美地解决了性能问题:
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = UIScreen.mainScreen.scale;
答案 5 :(得分:1)
以下是其中一个解决方案:
@IBOutlet private weak var blockView: UIView! {
didSet {
blockView.backgroundColor = UIColor.white
blockView.layer.shadowColor = UIColor.black.cgColor
blockView.layer.shadowOpacity = 0.5
blockView.layer.shadowOffset = CGSize.zero
blockView.layer.cornerRadius = 10
}
}
@IBOutlet private weak var imageView: UIImageView! {
didSet {
imageView.layer.cornerRadius = 10
imageView.layer.masksToBounds = true
imageView.layer.shouldRasterize = true
}
}