#import "ViewController.h" @interface ViewController () { CAGradientLayer *_gradientLayer; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]]; _imageView.center = self.view.center; [self.view addSubview:_imageView]; _gradientLayer = [CAGradientLayer layer]; _gradientLayer.frame = _imageView.bounds; /**设置渐变方向*/ _gradientLayer.startPoint = CGPointMake(0, 0); _gradientLayer.endPoint = CGPointMake(0, 1); /**设置颜色*/ _gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor redColor].CGColor ]; /**设置颜色分割点*/ _gradientLayer.locations = @[@(0.5),@(1.0f)]; [_imageView.layer addSublayer:_gradientLayer]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES]; } - (void)timerEvent { _gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0].CGColor ]; }