昨天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)!]it
// let gradientLocations:[CGFloat] = [0,1]io
let gradientLayer:CAGradientLayer = CAGradientLayer()im
gradientLayer.colors = gradientColorstop
// gradientLayer.locations = gradientLocations as [NSNumber]?di
gradientLayer.frame = CGRect.init(x: 0, y: -20, width: SCREENW, height: 64)vi
// gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)co
// 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)