views:

52

answers:

1

I have a custom UITableView cell which has a button and a label. I fire a method when someone taps on the button, and then color that row. It's all working fine.

What I want to actually do is

  1. user taps button, the row is colored in a gradient (it works now)
  2. The gradient fades away

My code is below (BackView is the 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 the button on the row, the background gets a gradient, but it never fades away, there's no animation - nothing. What am I doing wrong? I tried a few permutations and saw some examples, but none that helped me get this working.

Thanks!

+1  A: 

When you animate a property with an explicit animation, you have provide the type that property is expecting. The colors property is animatable, however, you are giving it a UIColor for the from and to values. It's expecting an NSArray. Also, you need CGColorRefs for the colors themselves. I haven't tried it, but I'm thinking you need to change your to and from lines to:

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 the effect of fading out.

Best regards.

Matt Long
Matt,Yes! That was exactly the problem and your answer resolved my question . I've hit another snag - now the fade works nicely, but only once. This is a row in a tableview. When I tap a button in a cell, I see this gradient/fade the first time. All subsequent taps do fire the method where I create the layer etc. but nothing happens visually. I suspect it's got something to do with maybe the layer, but don't know enough. I did try the replaceSubLayer but couldn't get it to work, any hints? I will plough forward anyhow - but thanks a bunch for the reply.
John
If you hold onto a pointer to the gradient layer within your cell as an ivar, you can just remove it when the animation completes and then re-create/re-add it the next time you want to animate it.
Matt Long
hmm..that makes sense. Will do - thanks.
John