19

我创建了一个自定义容器控制器,其工作方式与 a 类似,UIPageViewController以便我可以实现一些自定义转换和数据源逻辑。我试图模仿新的客户视图控制器转换 API 在 iOS 7 中的工作方式,它运行良好,除了在取消转换时视图外观回调的一些恼人的怪癖......

即,在执行转换时,究竟应该何时调用beginAppearanceTransition:animated:和?endAppearanceTransition


我的自定义容器类有一些这样的代码:

- (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] && [self.transitionController conformsToProtocol:@protocol(UIViewControllerInteractiveTransitioning)]) {
        // Start the interactive transition.
        [self.transitionController startInteractiveTransition:context];
    } else if ([context isAnimated] && [self.transitionController conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)]) {
        // Start the animated transition.
        [self.transitionController animateTransition:context];
    } else {
        // Just complete the transition.
        [context completeTransition:YES];
    }
}

因此,如果我endAppearanceTransition不管转换是否被取消都调用,那么当转换被取消时,我的视图回调看起来像这样:

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.

如果我endAppearanceTransition 在转换成功完成时调用,一开始情况会更好:

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)

但是,下次我开始转换时,我没有收到任何视图外观回调。下一组视图外观回调仅在调用之后beginAppearanceTransition:animated:到达endApperanceTransition。值得注意的是,我没有收到经常报告的“对 ViewController 的开始/结束外观转换的不平衡调用”控制台警告。

WWDC 2013 Session 218 (Custom Transitions Using View Controllers)中,Bruce Nilo 开了一个相当中肯的玩笑,说他的同事告诉他viewWillAppear:&viewWillDisappear:真的应该被称为viewMightAppear:& viewMightDisappear: (参见从 42:00 开始的那个会话部分)。鉴于我们现在处于可取消交互手势的领域,似乎我们需要一个自定义容器的cancelAppearanceTransition(或endAppearanceTransition:(BOOL)finished)方法。

有人知道我做错了什么吗?或者自定义容器中的可取消自定义转换还没有得到正确支持?

4

1 回答 1

23

因此,通常情况下,您会花费数小时徒劳地解决问题,然后在发布问题后立即找到答案......

UINavigationController我查看了内置的外观回调,interactivePopGestureRecognizer看看取消转换时会发生什么,看来我对应该调用哪些外观方法的假设是错误的。

对于成功转换到的视图控制器,会收到以下回调(如您所料):

                 transition
                  finished
viewWillAppear:  ---------->  viewDidAppear:

但是对于不成功的视图控制器转换(即转换被取消),视图控制器会收到以下回调:

                 transition                       immediately
                 cancelled                        followed by
viewWillAppear:  ---------->  viewWillDisappear:  ---------->  viewDidDisappear:

所以我们得到了更多那些狡猾的视图外观语义;我不确定一个视图如果还没有出现怎么会消失!哦,好吧...我想这比说它出现然后什么都没有发生的视图要好一些。

所以,回到我CustomTransitionContexttransitionCompletionHandler代码,解决方案如下所示:

...

// Register a completion handler to run when the context's completeTransition: method is called.
__weak CustomContainerController *weakSelf = self;
__weak CustomTransitionContext *weakContext = context;
context.transitionCompletionHandler = ^(BOOL complete) {
    if (complete) {
        // End the appearance transitions we began earlier.
        [oldVC endAppearanceTransition];
        [newVC endAppearanceTransition];

        [oldVC removeFromParentViewController];
        [newVC didMoveToParentViewController:weakSelf];
    } else {
        // Before ending each appearance transition, begin an
        // appearance transition in the opposite direction.
        [newVC beginAppearanceTransition:NO animated:[weakContext isAnimated]];
        [newVC endAppearanceTransition];
        [oldVC beginAppearanceTransition:YES animated:[weakContext isAnimated]];
        [oldVC endAppearanceTransition];

        [newVC removeFromParentViewController];
        [oldVC didMoveToParentViewController];
    }
}

....

所以现在外观回调看起来像这样:

oldVC viewWillDisappear:
newVC viewWillAppear:
// ... some time later transition is cancelled ...
newVC viewWillDisappear:
newVC viewDidDisappear:
oldVC viewWillAppear:
oldVC viewDidAppear:

此外,随后的转换现在按预期触发回调(因为我们通过调用来关闭所有转换endAppearanceTransition)。

于 2014-01-23T13:06:17.733 回答