title: How to 'cancel' view appearance transitions for custom container controller transitions
body: <p>I have created a custom container controller that works similarly to a <code>UIPageViewController</code> so that I could implement some custom transitions &amp; data source logic. I tried to mimic the way the new customer view controller transition APIs work in iOS 7, and it works well except for some irritating quirks with the view appearance callbacks when a transition is <strong><em>cancelled</em></strong>...</p>
  <h3>Namely, when performing a transition, when exactly should <code>beginAppearanceTransition:animated:</code> and <code>endAppearanceTransition</code> be called?</h3>
  <hr />
  <p>My custom container class has some code like this:</p>
  <pre><code>- (BOOL)shouldAutomaticallyForwardAppearanceMethods
  {
      return NO;  // Since the automatic callbacks are wrong for our custom transition.
  }
  
  - (void)startTransition:(CustomTransitionContext *)context
  {
      // Get reference to the view controllers involved in the transition.
      UIViewController *oldVC = [context viewControllerForKey:UITransitionContextFromViewController];
      UIViewController *newVC = [context UITransitionContextToViewController];
      
      // Prepare parent/child relationship changes.
      [oldVC willMoveToParentViewController:nil];
      [self addChildViewController:newVC];
  
      // Begin view appearance transitions.
      [oldVC beginAppearanceTransition:NO animated:[context isAnimated]];
      [newVC beginAppearanceTransition:YES animated:[context isAnimated]];
      
      // Register a completion handler to run when the context's completeTransition: method is called.
      __weak CustomContainerController *weakSelf = self;
      context.transitionCompletionHandler = ^(BOOL complete) {
          // End appearance transitions here?
          [oldVC endAppearanceTransition];
          [newVC endAppearanceTransition];
  
          if (complete) {
              // Or only if the transition isn't cancelled; here?
              [oldVC endAppearanceTransition];
              [newVC endAppearanceTransition];
  
              [oldVC removeFromParentViewController];
              [newVC didMoveToParentViewController:weakSelf];
          } else {
              [newVC removeFromParentViewController];
              [oldVC didMoveToParentViewController];
          }
      }
  
      if ([context isInteractive] &amp;&amp; [self.transitionController conformsToProtocol:@protocol(UIViewControllerInteractiveTransitioning)]) {
          // Start the interactive transition.
          [self.transitionController startInteractiveTransition:context];
      } else if ([context isAnimated] &amp;&amp; [self.transitionController conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)]) {
          // Start the animated transition.
          [self.transitionController animateTransition:context];
      } else {
          // Just complete the transition.
          [context completeTransition:YES];
      }
  }
  </code></pre>
  <p>So if I call <code>endAppearanceTransition</code> regardless of whether the transition was cancelled, then my view callbacks look like this when a transition is cancelled:</p>
  <pre><code>oldVC viewWillDisappear:  // Fine
  newVC viewWillAppear:     // Fine
  // ... some time later transition is cancelled ...
  oldVC viewDidDisappear:   // Wrong! This view controller's view is staying.
  newVC viewDidAppear:      // Wrong! The appearance of this view controllers view was cancelled.
  </code></pre>
  <p>If I call <code>endAppearanceTransition</code> <em>only</em> when the transition finished successfully, things look better at first:</p>
  <pre><code>oldVC viewWillDisappear:  // Fine
  newVC viewWillAppear:     // Fine
  // ... some time later transition is cancelled ...
  // ... silence. (which is correct - neither view actually appeared or disappeared,
  //               and I can undo side effects in viewWill(Dis)Appear using the 
  //               transitionCoordinator object)
  </code></pre>
  <p>But then, the next time I start a transition, I receive <strong>no view appearance callbacks</strong>. The next set of view appearance callbacks arrive only after a <code>beginAppearanceTransition:animated:</code> that is followed by an <code>endApperanceTransition</code> call. It is worth noting that I <strong>don't</strong> receive the oft-reported &quot;<em>Unbalanced calls to begin/end appearance transitions for ViewController</em>&quot; console warning.</p>
  <p>In <a href="https://developer.apple.com/wwdc/videos/" rel="noreferrer">WWDC 2013 Session 218 (Custom Transitions Using View Controllers)</a> Bruce Nilo makes a rather pertinent joke about his colleagues telling him that <code>viewWillAppear:</code> &amp; <code>viewWillDisappear:</code> really ought to be called <code>viewMightAppear:</code> &amp; <code>viewMightDisappear:</code> <em>(see the section of that session beginning at 42:00)</em>. Given that we're now in the realm of cancellable interactive gestures, it seems that we need a <code>cancelAppearanceTransition</code> (or <code>endAppearanceTransition:(BOOL)finished</code>) method for custom containers.</p>
  <h3>Is anybody aware of what I'm doing wrong? Or are cancellable, custom transitions in custom containers just not supported properly yet?</h3>