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