iOS设置UIView阴影遇到的一些坑

img

目的是为了给这块view下半部分加上阴影,实现代码如下。

topView.layer.masksToBounds = false
topView.layer.shadowOffset = CGSize.init(width: 0, height: 3)
topView.layer.shadowOpacity = 0.3
topView.layer.shadowRadius = 3
topView.layer.shadowColor = ViewUitl.colorWithHexString(hex: "#6691FB").cgColor
topView.layer.cornerRadius = 5
topView.layer.borderWidth = 1
topView.layer.borderColor = UIColor.white.cgColor

1坑

masksToBounds默认为false,也许项目中加了默认为true的效果。true的情况会导致阴影效果一直不会出来。
clipsToBounds默认也是false,最好也设置一下false,防止不出阴影效果。

2坑

shadowOffsetCGSize实现的,实际功能是偏移量。width是整个阴影x偏移几个像素,height是整个阴影y偏移几个像素。

这个属性要配合shadowRadius使用,比如我半径Radius设置是3,我想实现下半部分显示阴影,我要设置shadowOffset的height为3,这样上部分的阴影向下偏移3个像素,上半部分的阴影就看不到了。(如果height设置为-3的话,就是下半部分隐藏了,向上移动了3个像素)

解释

masksToBoundslayer对子layer进行切割,为true后切割后,阴影就看不到了。
shadowOffsetlayer阴影的偏移量设置。
shadowOpacity阴影的不透明度。
shadowRadius阴影的半径。
shadowColor阴影的颜色,会随着不透明度变。
cornerRadiusview的圆角弧度。
borderWidthview的边线宽度。
borderColorview的边线颜色。