在View上设置渐变颜色的效果
@interface ColorLineVC () @property (nonatomic, strong) UIView *backView; @end @implementation ColorLineVC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"颜色渐变"; [self backView]; } - (UIView *)backView{ if (!_backView) { _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 300, 300)]; CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init]; gradientLayer.colors = @[(__bridge id)[UIColor whiteColor].CGColor,(__bridge id)[UIColor blueColor].CGColor]; //位置x,y 自己根据需求进行设置 使其从不同位置进行渐变 gradientLayer.startPoint = CGPointMake(1.5, -1); gradientLayer.endPoint = CGPointMake(0, 1); gradientLayer.frame = CGRectMake(0, 0, 300, 300); [self.backView.layer addSublayer:gradientLayer]; [self.view addSubview:_backView]; } return _backView; }设置多颜色渐变
只要改变gradientLayer.colors,就可以啦
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor orangeColor].CGColor];