开发者

Animating UILabel opacity using CALayer?

I am animating the alpha of a UILabel so that it flashes white when a button is pressed. The label is pure white [r255,g255,b255,a1] to achieve the flash I am animating the CALayer opacity from 0.5 to 1.0 and then back to 0.5. The code to do this: (thanks to Dave DeLong for the help) is:

UILabel *navTitle;
@property(nonatomic, retain) UILabel *navTitle;
...
...
@synthesize navTitle;

.

// ADD ANIMATION OBJECT
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[anim setFromValue:[NSNumber numberWithFloat:0.5]];
[anim setToValue:[NSNumber numberWithFloat:1.0]];
[anim setAutoreverses:YES];
[anim setDuration:0.5];
[[[self navTitle] layer] addAnimation:anim forKey:@"flash"];
[[self navTitle] setTag:1138];

As the flash gets called multiple times (i.e. each time the button is pressed) I am calling removeAnimationForKey before the next flash, my question, is this开发者_如何学Python correct (i.e. the bit where I remove the animation from the layer). If I did not remove the layer am I right in assuming that they will just build up as I add more and more?

// REMOVE ANIMATION OBJECT
if([[self navTitle] tag] == 1138) {
    [[[self navTitle] layer] removeAnimationForKey:@"flash"];
    [[self navTitle] setTag:0];
}

NB: The initial idea was to do a constant pulsing (on a NSTimer) but on testing a single pulse looked at lot cleaner on the UI.

EDIT:

if you try to call removeAnimationForKey for a key that does not exist what happens, currently I am checking the UILabel tag before I do the remove, do I need to do this?


I think anyway, it is not wrong, it might be that the framework does it by its own way, but doing it manually doesn't harm it, as long as addAnimation method is a pair method with removeAnimation...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜