CAGradientLayer
优点:
- 可生成两种或更多种颜色平滑过渡效果
- 可通过Core Graphics拷贝CAGradientLayer的内容到普通图层 。
- CAGradientLayer使用硬件加速(硬件加速的好处无须多言)
__bridge
CF和OC对象转化时只涉及对象类型不涉及对象所有权的转化
更多知识:__bridge有关博客
1. 基础渐变
(因为真的很简单,所以看代码比较直接)
- (void)viewDidLoad {
[super viewDidLoad];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.clipView.bounds;
[self.clipView.layer addSublayer:gradientLayer];
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor blueColor].CGColor];
gradientLayer.startPoint = CGPointMake(0.5, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
}
2. 多重渐变
- (void)viewDidLoad {
[super viewDidLoad];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.clipView.bounds;
[self.clipView.layer addSublayer:gradientLayer];
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor greenColor].CGColor];
gradientLayer.locations = @[@0.0,@0.25,@0.5]; //必须与colors数组数目一致
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
}