CABasicAnimation in CAGradientLayer not working - what am I doing wrong?

I have a custom UITableView cell that has a button and label. I run the method when someone clicks on a button and then color this line. Everything is working fine.

Actually i want

  • button for pressing buttons, the line is painted in a gradient (now it works)
  • The gradient fades.

My code is below (BackView is a view in my custom cell)

CAGradientLayer *layer = [CAGradientLayer layer]; layer.frame = BackView.bounds; UIColor *cOne = [UIColor paleYellowColor]; UIColor *cTwo = [UIColor whiteColor]; NSArray *colors = [NSArray arrayWithObjects:(id)cOne.CGColor, cTwo.CGColor, nil]; layer.colors = colors; NSNumber *stopOne = [NSNumber numberWithFloat:0.00]; NSNumber *stopTwo = [NSNumber numberWithFloat:0.8]; NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil]; layer.locations = locations; CABasicAnimation *animateLayer = [CABasicAnimation animationWithKeyPath:@"colors"]; animateLayer.fromValue = [UIColor paleYellowColor]; animateLayer.toValue = [UIColor whiteColor]; animateLayer.duration = 3.0; animateLayer.removedOnCompletion = YES; animateLayer.fillMode = kCAFillModeBoth; animateLayer.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; [layer addAnimation:animateLayer forKey:@"animation"]; [BackView.layer insertSublayer:layer atIndex:0]; 

With this code, when I touch a button in a line, the background gets a gradient, but it never disappears, there is no animation - nothing. What am I doing wrong? I tried a few permutations and saw some examples, but none of them helped me work in this.

Thanks!

+4
source share
1 answer

When you animate a property with explicit animation, you provide the type that the property expects. The colors property is animated, however you give it a UIColor for from and to values. He expects NSArray. In addition, you need CGColorRefs for the colors themselves. I have not tried, but I think you need to change your line to and from it:

 animateLayer.fromValue = [NSArray arrayWithObjects: (id)[[UIColor paleYellowColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; animateLayer.toValue = [NSArray arrayWithObjects: (id)[[UIColor whiteColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 

In theory, this should fade from your yellow / white gradient to white / white, which should give a fading effect.

Sincerely.

+6
source

Source: https://habr.com/ru/post/1316026/


All Articles