Core Animation is not working with "alpha" value

5argon picture 5argon · Sep 23, 2011 · Viewed 15.7k times · Source

Before this code, my movie pic alpha is set to 0,

CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
    [fadein setToValue:[NSNumber numberWithFloat:1.0]];
    [fadein setDuration:0.5];
    [[moviepic layer]addAnimation:fadein forKey:@"alpha"];

Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.

I've seen a code using UIView beginAnimations: around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?

Answer

ohho picture ohho · Sep 23, 2011
[CABasicAnimation animationWithKeyPath:@"opacity"];

UIView exposes this as alpha where as CALayer exposes this as opacity.