Ios 为什么masksToBounds=YES可以防止CALayer阴影?
在下面的代码片段中,我将在我的UIView中添加一个阴影效果。这很有效。但只要我将视图的masksToBounds属性设置为YES。不再渲染阴影效果Ios 为什么masksToBounds=YES可以防止CALayer阴影?,ios,objective-c,uiview,calayer,Ios,Objective C,Uiview,Calayer,在下面的代码片段中,我将在我的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 =
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;
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;// 因为阴影是在视图外部完成的效果,而将masksToBounds设置为“是”将告诉UIView不要绘制自身外部的任何内容
如果您想要带阴影的圆角视图,我建议您使用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];
现在是iOS 6,事情可能已经改变了。在我设法再添加一行view2.layer.masksToBounds=NO之前,squad的答案对我不起作用代码>,否则阴影不会显示。虽然文档中说默认情况下,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];
编辑
如果视图需要设置动画或滚动,则会显著影响性能,这意味着动画可能会出现口吃。要获得圆角、阴影和平滑动画或滚动,请改用以下代码:
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];
我也有严重的阴影和圆角性能问题。我没有使用阴影路径部分,而是使用了以下几行,完美地解决了性能问题:
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = UIScreen.mainScreen.scale;
这是@TheSquad发布的答案的Swift 3和IB可设计版本
我在脚本文件中进行更改时使用了相同的概念。首先,我将我的目标视图(需要角半径和阴影的视图)移动到一个新的容器视图中。然后,我添加了以下代码行(参考:),为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
}
}
添加此代码后,我回到情节提要,选择mycontainerView后,我现在可以在属性检查器中找到一组新的属性:
除了根据我的选择为这些属性添加值之外,我还向我的targetView添加了一个角半径,并将masksToBounds属性设置为true
我希望这能有所帮助:)Swift 3.0版本和故事板
与@TheSquad的想法相同。在实际视图下创建新视图,并将阴影添加到下部视图
1。在实际视图下创建视图
将ui视图
拖动到与目标视图具有相同约束的情节提要。选中要为目标视图绑定的剪辑。还要确保新视图列在目标视图之前,以便目标视图覆盖新视图
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
以下是解决方案之一:
@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
}
}
非常感谢您。。。这对我帮助很大。谢谢你……)请注意,确保superview[本例中为view2]具有清晰的背景色,这是我的问题anyway@GangstaGraham确保将view2添加为view1的子视图。这正是我所需要的+第一个答案是1。您更新的答案不适用于圆角。。如果将“masksToBounds”设置为“NO”,则圆角将消失。您可以使用“shouldRasterize”属性来获得良好的性能,而不是更改该属性。这项功能现在可以在2020年使用。所以简单的解决方案就是添加一行。但是,不能用这个来设置动画或重新播放屏幕。