
img
目的是為了給這塊view下半部分加上陰影,實現(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默認為false,也許項目中加了默認為true的效果。true的情況會導致陰影效果一直不會出來。
clipsToBounds默認也是false,最好也設置一下false,防止不出陰影效果。
2坑
shadowOffset是CGSize實現(xiàn)的,實際功能是偏移量。width是整個陰影x偏移幾個像素,height是整個陰影y偏移幾個像素。
這個屬性要配合shadowRadius使用,比如我半徑Radius設置是3,我想實現(xiàn)下半部分顯示陰影,我要設置shadowOffset的height為3,這樣上部分的陰影向下偏移3個像素,上半部分的陰影就看不到了。(如果height設置為-3的話,就是下半部分隱藏了,向上移動了3個像素)
解釋
masksToBoundslayer對子layer進行切割,為true后切割后,陰影就看不到了。
shadowOffsetlayer陰影的偏移量設置。
shadowOpacity陰影的不透明度。
shadowRadius陰影的半徑。
shadowColor陰影的顏色,會隨著不透明度變。
cornerRadiusview的圓角弧度。
borderWidthview的邊線寬度。
borderColorview的邊線顏色。