我正在尝试将圆角半径添加到bottmLeft和bottomRight角落,并为其添加阴影。但由于某种原因,如果我添加角落半径,阴影就会消失。是什么原因造成的?
这就是我的所作所为:
我有扩展名:
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
self.layoutIfNeeded()
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
应用角落:
myView.roundCorners([.bottomLeft, .bottomRight], radius: 35
)
添加阴影扩展名:
func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float) {
let layer = self.layer
layer.masksToBounds = false
layer.shadowOffset = offset
layer.shadowColor = color.cgColor
layer.shadowRadius = radius
layer.shadowOpacity = opacity
layer.shadowPath = UIBezierPath.init(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath
let backgroundCGColor = self.backgroundColor?.cgColor
self.backgroundColor = nil
layer.backgroundColor = backgroundCGColor
}
添加阴影:
myView.addShadow(offset: CGSize.init(width: 0.0, height: 10.0) , color: UIColor.blue, radius: 35.0, opacity: 1.0)
为什么添加圆角后阴影会消失?
答案 0 :(得分:0)
myView.layer。 shadowPath 。只需给它与myView相同的框架和边界。