UIViewController自定义转换和状态恢复

时间:2014-03-12 18:43:28

标签: ios objective-c uiviewcontroller state-restoration custom-transition

我有一个使用自定义转换的视图控制器,它运行良好。它利用了以下事实:当使用UIModalPresentationCustom时,不会移除呈现视图控制器,并将新视图控制器放在透明度上。

但是,当将其与状态恢复结合使用时,会移除呈现视图控制器。看起来因为视图控制器没有动画,所以从不调用自定义转换代码。有没有办法激活自定义转换,即使视图控制器没有动画?

- (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;
            }];
        }];
    }
}

1 个答案:

答案 0 :(得分:0)

我无法看到如何在状态恢复中从层次结构中删除presentationVC,但有用的是将以下内容放入您的应用委托中:

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

    return nil;
}

这样,如果您没有实现自定义状态恢复类并依赖Storyboard为您执行此操作,则可以遵循状态恢复逻辑。这可能会为思想提供食物。