全方位阴影 [英] Solid Shadow in All Directions

查看:108
本文介绍了全方位阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种设置图层的shadow属性的方法,例如:

I've been looking for a way to set the shadow property of a layer, like this:

其中浅蓝色是图层(在本例中为UIImageView),深蓝色是阴影.

where the light blue is the layer—the UIImageView in this instance—and the dark blue is the shadow.

我想做些事情:

  • 出现在所有四个方面:通过将shadowOffset设置为CGSizeMake(0.0, 0.0)并将shadowRadius设置为非零
  • 完全不透明:通过将shadowOffset设置为图层中的特定点并将shadowRadius设置为零来实现
  • Appear on all four sides: accomplished by setting shadowOffset to CGSizeMake(0.0, 0.0) and making shadowRadius nonzero
  • Be entirely opaque: accomplished by setting shadowOffset to a specific point in the layer and shadowRadius to zero

我已经将shadowOpacity设置为零,但是我找不到解决这两个问题的方法.例如,如果我有:

I already have shadowOpacityset to zero, but I can't find a way to do both of these. For example, if I have:

imageView.layer.shadowColor = UIColor.orangeColor().CGColor
imageView.layer.shadowOffset = CGSizeMake(4.0 , 4.0)
imageView.layer.shadowOpacity = 1;
imageView.layer.shadowRadius = 0
imageView.layer.masksToBounds = false

我在两侧都留下了坚实的阴影.如果我希望阴影在所有四个侧面上,我将shadowOffset更改为特定大小,并将shadowRadius更改为大于零的内容,但是它不是很牢固.

I get a solid shadow on two sides. If I want the shadow to be on all four sides, I change shadowOffset to a specific size and shadowRadius to something greater than zero, but then it's not solid.

推荐答案

另一种选择是使用borderWidthborderColor.请记住,如文档中所述,边框是在图层边界内 绘制的,而不是向外扩展,因此您可能需要适当调整其大小.

An alternative option is to use borderWidth and borderColor. Keep in mind that, as mentioned in the docs, borders are drawn inside the layer bounds, rather than extending outside, so you may need to adjust its size as appropriate.

(根据您的用例,您可能会考虑将UIImageView与insets一起使用来制作可拉伸图像,如果您调整图层的大小,则可能具有不同的性能特征.)

(Depending on your use case, you might consider using UIImageView with insets to make a stretchable image, which may have different performance characteristics if your layer is being resized.)

这篇关于全方位阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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