title: Animating transitions of two subviews simultaneously with CATransition
body: <p>I have my main view, and when a user taps a 'Show' button I want two things to happen:</p>
  
  <ol>
  <li>Fade in a black <code>UIView</code> with alpha 0.5 covering the entire parent
  view. I'll refer to this as <code>darkenBackground</code></li>
  <li>Slide the view of a second view controller
  (<code>self.secondViewController.view</code>) on top of <code>darkenBackground</code></li>
  </ol>
  
  <p>When a user is finished, they tap 'Done' and the following happens:</p>
  
  <ol>
  <li><code>darkenBackground</code> fades out and is removed from the superview.</li>
  <li><code>self.secondViewController.view</code> slides out of the screen in the
  bottom direction and is removed from the superview.</li>
  </ol>
  
  <p>All animations working simultaneously.</p>
  
  <p>So far, I've been able to achieve the first part (sort-of), transition-in. This works fine. But when the user taps 'Done' the transition-out does not work as required. The problems I'm faced with are:</p>
  
  <ol>
  <li><code>darkenBackground</code> and <code>self.secondViewController.view</code> fade out and are removed, but
  <code>self.secondViewController.view</code> wasn't supposed to fade out! I tried animating only the second view but it simply disappears without animation.</li>
  <li>When a user taps 'Show' for a <strong>second</strong> time
  (transition-in), the view from the previous transition
  appears before the animation takes place. It doesn't look like it
  was removed from the superview previously.</li>
  </ol>
  
  <p>Here is my code:</p>
  
  <p>When a user taps 'Show':</p>
  
  <pre><code>- (IBAction)showSecondView {
  
      darkenBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
      [darkenBackground setBackgroundColor:[UIColor blackColor]];
      [darkenBackground setAlpha:0.5];
  
      self.secondViewController.view.frame = CGRectMake(0, 0, 320, 460);
      self.secondViewController.view.backgroundColor = [UIColor clearColor];
  
      [CATransaction begin];
  
      CATransition *animation = [CATransition animation];
      [animation setDuration:0.5];
      [animation setType:kCATransitionFade];
  
      CATransition *animation2 = [CATransition animation];
      [animation2 setDuration:0.5];
      [animation2 setType:kCATransitionMoveIn];
      [animation2 setSubtype:kCATransitionFromTop];
      [animation2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  
      [self.view addSubview:darkenBackground];
      [self.view addSubview:self.secondViewController.view];
  
      [[self.view layer] addAnimation:animation forKey:@"darkenBkgFadeIn"];
      [[self.secondViewController.view layer] addAnimation:animation2 forKey:@"ShowSecondView"];
  
      [CATransaction commit];
  }
  </code></pre>
  
  <p>When a user taps 'Done' (A function defined in the <code>secondViewController.m</code> class where it refers to the main view controller variables through delegate):</p>
  
  <pre><code>- (IBAction)hideSecondView {
  
      [CATransaction begin];
  
      CATransition *animation = [CATransition animation];
      [animation setDuration:0.5];
      [animation setType:kCATransitionFade];
  
      CATransition *animation2 = [CATransition animation];
      [animation2 setDuration:0.5];
      [animation2 setType:kCATransitionPush];
      [animation2 setSubtype:kCATransitionFromBottom];
      [animation2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  
      [self.myParentController.darkenBackground removeFromSuperview];
      [self.myParentController.secondViewController.view removeFromSuperview];
  
      [[self.myParentController.view layer] addAnimation:animation forKey:@"darkenBkgFadeOut"];
      [[self.myParentController.secondViewController.view layer] addAnimation:animation2 forKey:@"RemoveSecondView"];
  
      [CATransaction commit];
  }
  </code></pre>
  
  <p>Ultimately I want to show the view in the same way that the <code>TWTweetComposeViewController</code> modal view appears, where the background darkens and the controller appears. I could just use a modal view controller, but the issue I have with that is that when presenting the view controller it only 'appears' and does not slide from the bottom (since I require a semi-transparent background, I cannot use the default context for the modal view).</p>
  
  <p>Cannot figure this one out, would appreciate some guidance.</p>