目的是為了給這塊view下半部分加上陰影蚓再,實(shí)現(xiàn)代碼如下站粟。
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
默認(rèn)為false浦妄,也許項(xiàng)目中加了默認(rèn)為true的效果。true的情況會(huì)導(dǎo)致陰影效果一直不會(huì)出來吴超。
clipsToBounds
默認(rèn)也是false钉嘹,最好也設(shè)置一下false,防止不出陰影效果鲸阻。
2坑
shadowOffset
是CGSize
實(shí)現(xiàn)的跋涣,實(shí)際功能是偏移量。width是整個(gè)陰影x偏移幾個(gè)像素赘娄,height是整個(gè)陰影y偏移幾個(gè)像素仆潮。
這個(gè)屬性要配合shadowRadius
使用,比如我半徑Radius設(shè)置是3遣臼,我想實(shí)現(xiàn)下半部分顯示陰影性置,我要設(shè)置shadowOffset
的height為3,這樣上部分的陰影向下偏移3個(gè)像素揍堰,上半部分的陰影就看不到了鹏浅。(如果height設(shè)置為-3的話嗅义,就是下半部分隱藏了,向上移動(dòng)了3個(gè)像素)
解釋
masksToBounds
layer對(duì)子layer進(jìn)行切割隐砸,為true后切割后之碗,陰影就看不到了。
shadowOffset
layer陰影的偏移量設(shè)置季希。
shadowOpacity
陰影的不透明度褪那。
shadowRadius
陰影的半徑。
shadowColor
陰影的顏色式塌,會(huì)隨著不透明度變博敬。
cornerRadius
view的圓角弧度。
borderWidth
view的邊線寬度峰尝。
borderColor
view的邊線顏色偏窝。