CAGradientLayer中的CABasicAnimation不起作用 - 我做错了什么?

Joh*_*ohn 5 iphone cocoa-touch core-animation

我有一个自定义的UITableView单元格,它有一个按钮和一个标签.当有人点击按钮时,我会触发一个方法,然后为该行着色.一切正常.

我想要真正做的是

  1. 用户点击按钮,该行以渐变着色(现在可以使用)
  2. 渐变消失了

我的代码如下(BackView是我的自定义单元格中的视图)

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];
Run Code Online (Sandbox Code Playgroud)

使用此代码,当我触摸行上的按钮时,背景会获得渐变,但它永远不会消失,没有动画 - 没有.我究竟做错了什么?我尝试了一些排列并看到了一些例子,但没有一个帮助我实现这一点.

谢谢!

Mat*_*ong 6

使用显式动画为属性设置动画时,您必须提供属性所期望的类型.colors属性是可动画的,但是,您要为from和to赋予它一个UIColor.它期待一个NSArray.此外,您需要CGColorRefs颜色本身.我没有尝试过,但我认为你需要改变你的行和从行到:

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];
Run Code Online (Sandbox Code Playgroud)

理论上,这应该从你的黄色/白色渐变淡白色/白色,这应该会产生淡出效果.

最好的祝福.