答案 0 :(得分:1)
向UIView添加扩展程序
extension UIView {
func fillGradiant() {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.frame.size.width = UIScreen.main.bounds.size.width;
gradient.colors = [UIColor(hex:"3067d5").cgColor,UIColor(hex:"009bdf").cgColor] //Choose your gradiant colors
gradient.startPoint = CGPoint.zero
gradient.endPoint = CGPoint(x: 1, y: 0)
self.layer.insertSublayer(gradient, at: 0)
}
}
现在,您可以通过调用此方法向特定的UIView添加渐变:
yourBubbleView.fillGradiant()
扩展UIColor以添加hex:
extension UIColor {
convenience init(hex: String) {
let scanner = Scanner(string: hex)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(
red: CGFloat(r) / 0xff,
green: CGFloat(g) / 0xff,
blue: CGFloat(b) / 0xff, alpha: 1
)
}
}