Solid Shadow in All Directions

坚强是说给别人听的谎言 提交于 2020-01-15 03:55:10

问题


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

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

I'd like the shadow to do things:

  • 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

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

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.


回答1:


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.

(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.)



来源:https://stackoverflow.com/questions/34583323/solid-shadow-in-all-directions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!