@property (nonatomic, strong)  NSTimer *timer; /**< 定时器 */
@property (strong, nonatomic) CAGradientLayer *gradientLayer; /**< 渐变层 */




    [self.layer insertSublayer:self.gradientLayer atIndex:0];
    // 初始化定时器
      self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                                                target:self
                                              selector:@selector(respondsToTimerEvent)
                                              userInfo:nil
                                               repeats:YES];





- (void)respondsToTimerEvent {

    // 设定颜色组动画
    
    self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
                                  (__bridge id)[UIColor colorWithRed:arc4random()%255 / 255.f
                                                               green:arc4random()%255 / 255.f
                                                                blue:arc4random()%255 / 255.f
                                                               alpha:1].CGColor];
    // 设置颜色分割点动画
    self.gradientLayer.locations = @[@(arc4random() %10 / 10.f), @(1.f)];
}





- (CAGradientLayer *)gradientLayer {
    if (!_gradientLayer) {

        // 初始化渐变层
        _gradientLayer       = [CAGradientLayer layer];
        _gradientLayer.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
        
        // 设置颜色渐变方向
        _gradientLayer.startPoint = CGPointMake(0, 0);
        _gradientLayer.endPoint   = CGPointMake(0, 1);

        // 设定颜色组
        _gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
                                  (__bridge id)[UIColor whiteColor].CGColor];

        // 设定颜色分割点
        _gradientLayer.locations = @[@(0.5f), @(1.f)];
    }
    return _gradientLayer;
}

 

 

参考:

https://blog.csdn.net/aotang2760/article/details/101111679

posted on 2023-09-23 14:43  懂事长qingzZ  阅读(79)  评论(0编辑  收藏  举报