CAGradientLayer

来源:互联网 发布:机械姬 知乎 编辑:程序博客网 时间:2024/05/10 13:29
原文地址:CAGradientLayer作者:子木潇雨

CAGradientLayer可以方便的处理颜色渐变。

Properties:

@property(copy) NSArray *colors

渐变颜色的数组

[cpp] viewplaincopy
  1. [NSArray arrayWithObjects:(id)[[[UIColor blackColor] colorWithAlphaComponent:1] CGColor],  
  2.                           (id)[[[UIColor yellowColor] colorWithAlphaComponent:1] CGColor],  
  3.                           (id)[[[UIColor blueColor] colorWithAlphaComponent:1] CGColor],  
  4.                   (id)[[UIColor clearColor] CGColor],  
  5.               nil];  

如上定义了四种颜色(最后一种是无色)。

@property(copy) NSArray *locations

渐变颜色的区间分布,locations的数组长度和color一致,这个值一般不用管它,默认是nil,会平均分布。
[cpp] viewplaincopy
  1. [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],  
  2.                           [NSNumber numberWithFloat:0.3],  
  3.                           [NSNumber numberWithFloat:0.8],  
  4.                           [NSNumber numberWithFloat:1.0],  
  5.                           nil];  
注意这几个数字在0到1之间单调递增。

@property CGPoint startPoint

映射locations中第一个位置,用单位向量表示,比如(0,0)表示从左上角开始变化。默认值是(0.5,0.0)。

@property CGPoint endPoint

映射locations中最后一个位置,用单位向量表示,比如(1,1)表示到右下角变化结束。默认值是(0.5,1.0)。

@property(copy) NSString *type

默认值是kCAGradientLayerAxial,表示按像素均匀变化。除了默认值也无其它选项。


下面是我用上面的代码实现的最终效果,startPoint是(0,0),endPoint是(1,1)。

0 0
原创粉丝点击