2

我有一个使用自定义转换的视图控制器,它运行良好。它利用了这样一个事实,即在使用 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;
            }];
        }];
    }
}
4

1 回答 1

0

我看不到如何在状态恢复时从层次结构中删除您的presentingVC,但是将以下内容放入您的应用程序委托中会有所帮助:

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

    return nil;
}

这样,如果您不实现自定义状态恢复类并依靠 Storyboard 为您执行此操作,您可以遵循状态恢复逻辑。这可能会让人深思。

于 2014-08-05T16:24:00.080 回答