昨天UI同事给的导航栏颜色渐变背景图,只有在plus上图片出现问题,其他的都是正常显示,总是找不到问题所在,后来问了同事后,说图片是用插件强行在2x图片下切得3x图片,在plus上图片出现问题,后来,找了资料发现,Core Animation框架中提供CAGradientLayer可以很方便的处理颜色渐变效果;
let topColor = UIColor.init(hexString: "02ccca")//02ccca
let bottomColor = UIColor.init(hexString: "10e6a7")//10e6a7
let gradientColors:[CGColor] = [(topColor?.cgColor)!,(bottomColor?.cgColor)!]
// let gradientLocations:[CGFloat] = [0,1]
let gradientLayer:CAGradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColors
// gradientLayer.locations = gradientLocations as [NSNumber]?
gradientLayer.frame = CGRect.init(x: 0, y: -20, width: SCREENW, height: 64)
// gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
// let endX = 1 - 64/SCREENW
// print("endX:\(endX)")
// gradientLayer.endPoint = CGPoint.init(x: endX, y: 1)
gradientLayer.startPoint = CGPoint.init(x: 1, y: 1)
let endX = 1 - 64/SCREENW
print("endX:\(endX)")
gradientLayer.endPoint = CGPoint.init(x: endX, y: 0)
self.navigationBar.layer.insertSublayer(gradientLayer, at: 0)