CAGradientLayer是用来生成一个或多个颜色的渐变,属于QuartzCore框架
下面是使用CAGradientLayer来生成一个简单的渐变色的示例
- (CAGradientLayer *)setBackgroundLayer
{
//cagradientlayer是继承自calayer的类
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
//设置gradientLayer的颜色,是一个数组,你可以设置多个颜色
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id) [UIColor yellowColor].CGColor, (__bridge id)[UIColor greenColor].CGColor];
//set locations
gradientLayer.locations = @[@0.0, @.5, @1];//如果不设置,颜色会在指定范围内均匀分布
//set gradient start and end points
gradientLayer.startPoint = CGPointMake(0, 0);//默认是(.5,0)
gradientLayer.endPoint = CGPointMake(1, 0);//默认是(.5,1)
return gradientLayer;
}
其中涉及的bridge类型桥接详情见IOS 之__bridge,__bridge_transfer和__bridge_retained