CAGradientLayer 渐变图层 CALayer子类之一
渐变图层(画质貌似与问题).gif
CAGradientLayer 渐变图层的几大属性
colors:渐变颜色的数组 -> id -> CGColor
locations:颜色渐变的 百分比 数组
startPoint 颜色渐变的起始点
endPoint 颜色渐变的终点
颜色会根据 设置的两个点去变化
渐变图层是根据colors数组中的颜色在图层中根据渐变起始点一次分布在图层中的,但是并不会出现上图中一直变化的效果,这里我是使用了随机函数以及定时器来达到这种效果。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.image = [UIImage imageNamed:@"4"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
//CAGradientLayer 渐变
// colors:渐变颜色的数组 -> id -> CGColor
// locations:颜色渐变的 百分比 数组
// startPoint 颜色渐变的起始点
// endPoint 颜色渐变的终点
// 颜色会根据 设置的两个点 去变化
layer = [CAGradientLayer layer];
layer.frame = self.view.frame;
layer.opacity = 0.3;//透明度
// layer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor orangeColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor cyanColor].CGColor,(id)[UIColor blueColor].CGColor,(id)[UIColor purpleColor].CGColor];
// layer.locations = @[];
layer.startPoint = CGPointMake(0, 0);
layer.endPoint = CGPointMake(1, 1);
[self.view.layer addSublayer:layer];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(todo) userInfo:nil repeats:YES];
}
-(void)todo{
//设置随机数
int a = arc4random()%7;
int b = arc4random()%7;
int c = arc4random()%7;
int d = arc4random()%7;
NSArray *col = @[(id)[UIColor redColor].CGColor,(id)[UIColor orangeColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor cyanColor].CGColor,(id)[UIColor blueColor].CGColor,(id)[UIColor purpleColor].CGColor];
//给渐变数组随机的颜色值
layer.colors =@[col[a],col[b],col[c],col[d]];
NSLog(@"%ld%ld%ld%ld",a,b,c,d);
}