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

问题描述

我正在尝试向彼此叠置的视图添加阴影,这些视图折叠以允许查看其他视图中的内容,因此我想保持view.clipsToBounds ON以便当视图折叠其内容将被剪切.

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.

Swift 2.2:

Swift 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添加阴影的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:19