向我的 UIView 添加阴影的最佳方法是什么 [英] What's the best way to add a drop shadow to my UIView

查看:21
本文介绍了向我的 UIView 添加阴影的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为相互叠加的视图添加阴影,视图折叠允许看到其他视图中的内容,在这种情况下我想保留 view.clipsToBounds 开启以便在视图折叠时其内容被剪裁.

I am trying to add a drop shadow to views that are layered on top of one another, the views collapse allowing content in other views to be seen, in this vein i want to keep view.clipsToBounds ON so that when the views collapse their content is clipped.

这似乎让我很难为图层添加阴影,因为当我打开 clipsToBounds 时,阴影也被剪裁了.

This seems to have made it difficult for me to add a drop shadow to the layers as when i turn clipsToBounds ON the shadows are clipped also.

我一直在尝试操作 view.frameview.bounds 以便为框架添加阴影,但允许边界足够大以包含它,但是我没有运气.

I have been trying to manipulate view.frame and view.bounds in order to add a drop shadow to the frame but allow the bounds to be large enough to encompass it, however I have had no luck with this.

这是我用来添加阴影的代码(这只适用于 clipsToBounds OFF,如图所示)

Here is the code I am using to add a Shadow (this only works with clipsToBounds OFF as shown)

view.clipsToBounds = NO;
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowOffset = CGSizeMake(0,5);
view.layer.shadowOpacity = 0.5;

这是应用于最浅灰色层的阴影的屏幕截图.希望这能让我了解如果 clipsToBounds 关闭,我的内容将如何重叠.

Here is a screenshot of the shadow being applied to the top lightest grey layer. Hopefully this gives an idea of how my content will overlap if clipsToBounds is OFF.

如何为我的 UIView 添加阴影并保持我的内容被剪裁?

How can I add a shadow to my UIView and keep my content clipped?

只是想补充一点,我也尝试过使用带阴影的背景图像,效果很好,但是我仍然想知道最好的编码解决方案.

Just wanted to add that I have also played around with using background images with shadows on, which does work well, however I would still like to know the best coded solution for this.

推荐答案

试试这个:

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.5f;
view.layer.shadowPath = shadowPath.CGPath;

首先:用作shadowPathUIBezierPath至关重要.如果您不使用它,一开始您可能不会注意到差异,但敏锐的眼睛会观察到在旋转设备和/或类似事件期间发生的一定滞后.这是一项重要的性能调整.

First of all: The UIBezierPath used as shadowPath is crucial. If you don't use it, you might not notice a difference at first, but the keen eye will observe a certain lag occurring during events like rotating the device and/or similar. It's an important performance tweak.

特别针对您的问题:重要的是view.layer.masksToBounds = NO.它禁止对超出视图边界的视图层的子层进行裁剪.

Regarding your issue specifically: The important line is view.layer.masksToBounds = NO. It disables the clipping of the view's layer's sublayers that extend further than the view's bounds.

对于那些想知道 masksToBounds(在图层上)和视图自己的 clipToBounds 属性之间有什么区别的人来说:实际上没有任何区别.切换一个会对另一个产生影响.只是抽象层次不同.

For those wondering what the difference between masksToBounds (on the layer) and the view's own clipToBounds property is: There isn't really any. Toggling one will have an effect on the other. Just a different level of abstraction.

斯威夫特 2.2:

override func layoutSubviews()
{
    super.layoutSubviews()

    let shadowPath = UIBezierPath(rect: bounds)
    layer.masksToBounds = false
    layer.shadowColor = UIColor.blackColor().CGColor
    layer.shadowOffset = CGSizeMake(0.0, 5.0)
    layer.shadowOpacity = 0.5
    layer.shadowPath = shadowPath.CGPath
}

<小时>

斯威夫特 3:


Swift 3:

override func layoutSubviews()
{
    super.layoutSubviews()

    let shadowPath = UIBezierPath(rect: bounds)
    layer.masksToBounds = false
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
    layer.shadowOpacity = 0.5
    layer.shadowPath = shadowPath.cgPath
}

这篇关于向我的 UIView 添加阴影的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆