CAGradientlayer设置视图背景的渐变效果
可以使用CAGradientlayer来设置某些视图背景的渐变效果:
-(void)setGradientColor {
//初始化CAGradientlayer对象,使它的大小为需要设置背景色的视图的大小
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.bounds;
//设置渐变区域的起始和终止位置(范围为0-1)
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
//设置渐变颜色值集合,颜色将从(0, 0)至(0, 1)填充
gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:@"#383782"].CGColor,
(__bridge id)[UIColor blackColor].CGColor];
//设置颜色分割点范围集,与上面颜色数组对应(范围:0 - 1)
gradientLayer.locations = @[@(0.5f), @(1.0f)];
//将gradientLayer添加到需要设置背景色的视图layer层
[self.view.layer insertSublayer:gradientLayer atIndex:0];
}