class MineTopView: UIView {
override func draw(_ rect: CGRect) {
//避免重复绘制
layer.sublayers?.forEach({ obj in
if obj.isKind(of: CAGradientLayer.self) {
obj.removeFromSuperlayer()
}
})
let gradient = CAGradientLayer()
gradient.colors = [UIColor(red: 0.12, green: 0.61, blue: 0.93, alpha: 1).cgColor, UIColor(red: 0, green: 0.91, blue: 1, alpha: 1).cgColor]
gradient.frame = self.bounds
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.shadowColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.5).cgColor
gradient.shadowOffset = CGSize(width: 0, height: 0)
gradient.shadowOpacity = 0.8
gradient.shadowRadius = 10
gradient.cornerRadius = 8
layer.insertSublayer(gradient, at: 0)
}
}
网友评论