Creating an animated gradient background similar to the Solar Weather Application in xCode

前端 未结 2 1898
忘掉有多难
忘掉有多难 2021-01-30 18:43

Even though I have asked this question before, I would like to reach out again to clarify what I would like to accomplish with your help. I was wondering how you would create a

2条回答
  •  伪装坚强ぢ
    2021-01-30 19:33

    Simple working example:

    .h file

    @property (strong, nonatomic) CAGradientLayer *gradient;
    

    .m file

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear: animated];
    
        self.gradient = [CAGradientLayer layer];
        self.gradient.frame = self.view.bounds;
        self.gradient.colors = @[(id)[UIColor purpleColor].CGColor,
                                 (id)[UIColor redColor].CGColor];
    
        [self.view.layer insertSublayer:self.gradient atIndex:0];
    
        [self animateLayer];
    }
    
    -(void)animateLayer
    {
    
        NSArray *fromColors = self.gradient.colors;
        NSArray *toColors = @[(id)[UIColor redColor].CGColor,
                              (id)[UIColor orangeColor].CGColor];
    
        [self.gradient setColors:toColors];
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
    
        animation.fromValue             = fromColors;
        animation.toValue               = toColors;
        animation.duration              = 3.00;
        animation.removedOnCompletion   = YES;
        animation.fillMode              = kCAFillModeForwards;
        animation.timingFunction        = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.delegate              = self;
    
        // Add the animation to our layer
    
        [self.gradient addAnimation:animation forKey:@"animateGradient"];
    }
    

    You might want to implement an additional method to shift the colors, and then reanimate the changes. But this should help you get there.

提交回复
热议问题