ios UIView 的左右阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14219412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:45:58  来源:igfitidea点击:

Shadow left and right of UIView

iphoneioscalayershadow

提问by Zeropointer

Hi i like to add an CALayer Shadow to an View where the shadow was left and right of the view the simplest way was:

嗨,我喜欢向视图中添加一个 CALayer 阴影,其中阴影位于视图的左侧和右侧,最简单的方法是:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
someView.layer.shadowOpacity = 1.0f;
someView.layer.shadowRadius = 10.0f;
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:someView.bounds] CGPath];

but i will add a bigger shadow as like a shadow when i increase the shadowRadius it do not look good. How i can make a shadow that looks good at left and right.

但是当我增加 shadowRadius 时,我会添加一个更大的阴影,就像阴影一样,它看起来不太好。我如何制作左右两边都很好的阴影。

回答by progrmr

I think 10 is a pretty big shadow radius, try 3 or 4 instead, also opacity I usually use 0.7:

我认为 10 是一个相当大的阴影半径,请尝试使用 3 或 4,还有不透明度我通常使用 0.7:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
someView.layer.shadowOpacity = 0.7f;
someView.layer.shadowRadius = 4.0f;

If you want the shadow only on left and right, then inset the rectangle on the top and bottom so the top and bottom shadow are hidden behind your view:

如果您只想要左侧和右侧的阴影,则在顶部和底部插入矩形,以便顶部和底部阴影隐藏在您的视图后面:

CGRect shadowRect = CGRectInset(someView.bounds, 0, 4);  // inset top/bottom
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];

I'm not really sure if that's what you wanted.

我不确定这是否是你想要的。

回答by Fangming

swift 3.0 version

迅捷3.0版本

imageView.layer.shadowOpacity = 0.8
imageView.layer.shadowOffset = CGSize(width: 0, height: 3)
imageView.layer.shadowRadius = 4.0

let shadowRect: CGRect = imageView.bounds.insetBy(dx: 0, dy: 4)
imageView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath

回答by Johan Tingbacke

progrmr's answer was very helpful, cheers!

progrmr 的回答很有帮助,干杯!

I made a slide-out menu and I had a problem with the shadow surrounding my VC and disrupting the navigation bar. Turned out I had to inset the shadow layer.

我制作了一个滑出式菜单,但我的 VC 周围的阴影出现了问题并扰乱了导航栏。结果我不得不插入阴影层。

Here's my solution using swift:

这是我使用 swift 的解决方案:

rightViewController!.view.layer.shadowOpacity = 0.8
rightViewController!.view.layer.shadowOffset = CGSizeMake(0, 3)
rightViewController!.view.layer.shadowRadius = 4.0

let shadowRect: CGRect = CGRectInset(rightViewController!.view.bounds, 0, 4);  // inset top/bottom
rightViewController!.view.layer.shadowPath = UIBezierPath(rect: shadowRect).CGPath