代碼片段:
extension UIView {
// OUTPUT 1
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
// OUTPUT 2
func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOpacity = opacity
layer.shadowOffset = offSet
layer.shadowRadius = radius
layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
注意:如果你沒有通過任何參數該功能,那麼規模參數將默認爲true。您可以通過在該參數的類型之後爲該參數分配一個值來爲函數中的任何參數定義默認值。如果定義了默認值,則可以在調用該函數時省略該參數。
OUTPUT 1:
shadowView.dropShadow()
OUTPUT 2:
shadowView.dropShadow(color: .red, opacity: 1, offSet: CGSize(width: -1, height: 1), radius: 3, scale: true)
如果我想刪除這個陰影,如何M I應該做的? –
@SuhasPatil \t 如果你想去除陰影那麼,你爲什麼擺在首位加入呢?你能否詳細說明你的情況,以便我可以給你一個解決方案? –
實際上,對於單元格選擇,將會有陰影並取消選擇無陰影 –