title: Animating UILabel opacity using CALayer?
body: <p>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 <a href="https://stackoverflow.com/users/115730/dave-delong">Dave DeLong</a> for the help) is:</p>
  
  <pre><code>UILabel *navTitle;
  @property(nonatomic, retain) UILabel *navTitle;
  ...
  ...
  @synthesize navTitle;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>// 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];
  </code></pre>
  
  <p>As the flash gets called multiple times (i.e. each time the button is pressed) I am calling <code>removeAnimationForKey</code> before the next flash, my question, is this 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?</p>
  
  <pre><code>// REMOVE ANIMATION OBJECT
  if([[self navTitle] tag] == 1138) {
      [[[self navTitle] layer] removeAnimationForKey:@"flash"];
      [[self navTitle] setTag:0];
  }
  </code></pre>
  
  <p><strong><em>NB</strong>: 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.</em></p>
  
  <h1>EDIT:</h1>
  
  <p>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?</p>