我已经尝试了许多在overstackoverflow中可用的解决方案,但没有任何效果。这是我为dropShadow效果编写的扩展名。
extension UIView {
func dropShadow(color: UIColor, opacity: Float = 0.4, offSet: CGSize, shadowRadius: CGFloat = 1, scale: Bool = true, cornerRadius: CGFloat) {
let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
shadowLayer.fillColor = UIColor.white.cgColor
shadowLayer.shadowColor = color.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = offSet
shadowLayer.shadowOpacity = opacity
shadowLayer.shadowRadius = shadowRadius
layer.insertSublayer(shadowLayer, at: 0)
}
}
在这里,我使用的扩展名是:
override func layoutSubviews() {
contentView.backgroundColor = UIColor.clear
backgroundColor = UIColor.clear
backView.dropShadow(color: .black, offSet: CGSize(width: 0.0, height: 3.0), cornerRadius: 20)
backView.layer.cornerRadius = 20
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.5)
gradientLayer.frame.size = self.backView.frame.size
backView.layer.insertSublayer(gradientLayer, at:1)
}
这是我得到的结果:
正如您在屏幕快照中看到的那样,我正在获得阴影半径,但UIView半径仍然相同。那么如何获得那个角半径。我从早晨开始挣扎。任何帮助将不胜感激。预先感谢。
答案 0 :(得分:0)
我忘了添加gradientLayer的拐角半径。
override func layoutSubviews() {
contentView.backgroundColor = UIColor.clear
backgroundColor = UIColor.clear
backView.dropShadow(color: .black, offSet: CGSize(width: 0.0, height: 3.0), cornerRadius: 10)
backView.layer.cornerRadius = 20
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.5)
gradientLayer.cornerRadius = 10 . <--- this was missing
gradientLayer.frame.size = self.backView.frame.size
backView.layer.insertSublayer(gradientLayer, at:1)
}