title: How to fade UIView background color - iOS 5
body: <p>I'm having trouble trying to get a custom UIView class to fade it's background. I've checked out the following StackOverflow questions but they don't seem to work for me.</p>
  
  <p><a href="https://stackoverflow.com/questions/4785042/fade-background-color-from-one-color-to-another-in-a-uiview">Fade background-color from one color to another in a UIView</a></p>
  
  <p><a href="https://stackoverflow.com/questions/518192/how-do-you-explicitly-animate-a-calayers-backgroundcolor">How do you explicitly animate a CALayer&#39;s backgroundColor?</a></p>
  
  <p>So I have a custom UIView where users can draw things. If what they draw is incorrect, I want to make the background color fade to red then back to white.</p>
  
  <p>I have this custom method in the custom UIView called</p>
  
  <pre><code>- (void)indicateMistake;
  </code></pre>
  
  <p>When I call that method I want it to perform the background color animation, so i have this in the method:</p>
  
  <pre><code>CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
  fade.fromValue = (id)[UIColor whiteColor].CGColor;
  fade.toValue = (id)[UIColor redColor].CGColor;
  [fade setDuration:3];
  [self.layer addAnimation:fade forKey:@"fadeAnimation"];
  </code></pre>
  
  <p>But nothing seems to happen when I do that.</p>
  
  <p>So then I tried a silly rotation animation to see if it even works:</p>
  
  <pre><code>CABasicAnimation* rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  rotate.toValue = [NSNumber numberWithInt:M_PI];
  [rotate setDuration:1];
  [self.layer addAnimation:rotate forKey:@"rotateAnimation"];
  </code></pre>
  
  <p>For some reason that works and the custom UIView rotates.</p>
  
  <p>Then reading more StackOverflow answers I tried this:</p>
  
  <pre><code>[UIView animateWithDuration:3 animations:^{
      [self setBackgroundColor: [UIColor redColor]];
  } completion:^(BOOL finished) {
      [self setBackgroundColor: [UIColor whiteColor]];
  }];
  </code></pre>
  
  <p>That changes the color from red then back to white immediately. Sometimes It's so fast I sometimes can't see it happen. If i comment out the <code>[self setBackgroundColor: [UIColor whiteColor]];</code> it stays red. But There's node gradual white to red effect.</p>
  
  <p>I've ran out of ideas.. Any help would be appreciated!</p>