2

我有一個使用自定義過渡的視圖控制器,它運行良好。它利用了使用UIModalPresentationCustom的事實,不會刪除呈現視圖控制器,並將新視圖控制器放在透明度上。UIViewController自定義過渡和狀態恢復

但是,當與狀態恢復結合使用時,呈現視圖控制器被移除。看起來,因爲視圖控制器沒有動畫,所以自定義轉換代碼從不被調用。有沒有辦法讓自定義轉換被激活,即使視圖控制器沒有被動畫?

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented 
                    presentingController:(UIViewController *)presenting 
                     sourceController:(UIViewController *)source 
{ 
    // never called if we aren't animating 
    return self; 
} 

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed 
{ 
    return self; 
} 

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    return 0.25; 
} 

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 

    if (toViewController == self) { 
     [transitionContext.containerView addSubview:fromViewController.view]; 
     [transitionContext.containerView addSubview:toViewController.view]; 

     self.maskView.alpha = 0.0f; 
     self.menuView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 286.0f); 

     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
      fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; 

      [self.menuView updateFrame:^(CGRect *frame) { 
       frame->origin.y = self.view.bounds.size.height - frame->size.height; 
      }]; 

      self.maskView.alpha = 0.75f; 
     } completion:^(BOOL finished) { 
      self.maskView.userInteractionEnabled = YES; 
      [transitionContext completeTransition:YES]; 
     }]; 
    } else { 
     [transitionContext.containerView addSubview:toViewController.view]; 
     [transitionContext.containerView addSubview:fromViewController.view]; 

     self.maskView.userInteractionEnabled = NO; 

     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
      toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; 

      [self.menuView updateFrame:^(CGRect *frame) { 
       frame->origin.y = self.view.bounds.size.height; 
      }]; 

      self.maskView.alpha = 0.0f; 
     } completion:^(BOOL b){ 
      [transitionContext completeTransition:YES]; 

      [self.menuView updateFrame:^(CGRect *frame) { 
       frame->origin.y = self.view.bounds.size.height - frame->size.height; 
      }]; 
     }]; 
    } 
} 

回答

0

我看不出你的presentingVC可以從狀態恢復的層次結構被刪除,但會是什麼有用的是把你的應用程序代理如下:

- (UIViewController*)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder { 
    NSLog(@"Application restore state = %@", [identifierComponents componentsJoinedByString:@"."]); 

    return nil; 
} 

這樣你就可以按照狀態恢復邏輯,如果你沒有實現自定義狀態恢復類,並依靠Storyboard爲你做到這一點。這可能會爲思想提供食物。