属性
startPoint
和endPoint
决定渐变方向,以单位坐标系定义。左上角{0,0},右下角{1,1}colors
渐变的颜色,是一个CGColorRef
的数组。locations
定义了渲染的每个颜色的位置。取值在0到1之间。长度一定和colors
相同。
例子
- 多重渐变
var gradientLayer = CAGradientLayer.init() gradientLayer.colors = [UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6).CGColor,UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.9).CGColor,UIColor.init(white: 0.4, alpha: 0.5).CGColor] gradientLayer.colors = [UIColor.blackColor().CGColor,UIColor.yellowColor().CGColor,UIColor.blueColor().CGColor] gradientLayer.locations = [0.1,0.3,0.5] gradientLayer.frame = textLayer.bounds gradientLayer.startPoint = CGPointMake(1, 0) gradientLayer.endPoint = CGPointMake(1, 1) // textLayer.mask = gradientLayer self.contentView.layer.addSublayer(gradientLayer)
?
- 作为蒙版,实现文字渐变
var gradientLayer = CAGradientLayer.init() gradientLayer.colors = [UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6).CGColor,UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.9).CGColor,UIColor.init(white: 0.4, alpha: 0.5).CGColor] // gradientLayer.colors = [UIColor.blackColor().CGColor,UIColor.yellowColor().CGColor,UIColor.blueColor().CGColor] gradientLayer.locations = [0.1,0.3,0.5] gradientLayer.frame = textLayer.bounds gradientLayer.startPoint = CGPointMake(1, 0) gradientLayer.endPoint = CGPointMake(1, 1) textLayer.mask = gradientLayer
?
时间: 2024-10-12 02:56:34