绘制渐变色背景
2018-06-01 本文已影响0人
做个有趣的程序员
为满足设计需求,按钮背景颜色为渐变色,iOS 提供了CAGradientLayer 类来进行渐变色的设计
//layer进行渐变色设置
func nextBtnCagradientLayer() {
let btnLayer = CAGradientLayer.drawNextBtnGraduateColor(fromColor: "F73E46", toColor: "FBB13E")
btnLayer.frame = nextBtnEnabel.bounds
nextBtnEnabel.layer.insertSublayer(cagradientLayer,at: 1)
}
class func drawNextBtnGraduateColor(fromColor:String,toColor:String)->CAGradientLayer{
let gradentLayer = CAGradientLayer()
//创建渐变色数组,转换为CGColor
gradentLayer.colors = [UIColor.hexStringToColor(hexString:fromColor).cgColor,UIColor.hexStringToColor(hexString: toColor).cgColor]
gradentLayer.startPoint = CGPoint.init(x: 0, y: 0)
gradentLayer.endPoint = CGPoint.init(x: 1, y: 0)
//设置颜色变化点,取值范围 0.0~1.0
gradentLayer.locations = [0,1]
return gradentLayer
}
注意:使用SnpKit约束控件进行渐变色设置无效,设置控件的frame才能绘制出渐变色