0

I'd like to gradually change the background gradient from

gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];

to

gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];

[EDIT] My current code is:

                gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];
                NSArray *fromColors = gradient.colors;
                NSArray *toColors =[NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];
                CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"colors"];

                animation.fromValue = fromColors;
                animation.toValue = toColors;
                animation.duration = 3.00;
                animation.removedOnCompletion = false;
                animation.autoreverses = NO;
                animation.fillMode = kCAFillModeForwards;
                animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
                if (![gradient.animationKeys containsObject:@"colors"]) {
                    [gradient addAnimation:animation forKey:@"colors"];
                }
                [self.background.layer addSublayer:gradient];

But it doesn't do anything. Can someone help me please?

Community
  • 1
  • 1
Maray97
  • 140
  • 1
  • 11

1 Answers1

1

I don't think "colors" are supported in CABasicAnimation.keypath.But how to meet the requirement, I still can't think of the answer.https://stackoverflow.com/questions/5459673/how-can-i-know-the-values-in-cabasicanimation-keypath

Kris
  • 63
  • 3
  • Thank you, the trick is add "backgroundColor" as keypath – Maray97 Aug 13 '20 at 16:13
  • However, adding "backgroundColor" change the background to a one color background, not gradient. The keypath colors was right, but I had made a mistake: I had two gradients, self.gradient and gradient. Noob errors happen sometimes – Maray97 Aug 13 '20 at 23:24