CAGradientLayer渐变效果

属性

  1. startPointendPoint
    决定渐变方向,以单位坐标系定义。左上角{0,0},右下角{1,1}
  2. colors
    渐变的颜色,是一个CGColorRef的数组。
  3. locations
    定义了渲染的每个颜色的位置。取值在0到1之间。长度一定和colors相同。

例子

  1. 多重渐变

        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)

    CAGradientLayer渐变效果

  2. 作为蒙版,实现文字渐变

        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

    CAGradientLayer渐变效果

上一篇:Selector、shape详解,注意这两种图像资源都以XML方式存放在drawable不带分辨率的文件夹中


下一篇:JS传参中文乱码问题.NET