Iphone 正确使用从ViewController:到ViewController:持续时间:选项:动画:完成:

Iphone 正确使用从ViewController:到ViewController:持续时间:选项:动画:完成:,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,我似乎找不到一个关于如何正确使用从ViewController:toViewController:duration:options:animations:completion:转换的好例子 这是正确的吗?(假设我想将一个VC与另一个VC交换) 文档中没有明确说明何时调用什么: addChildViewController:方法调用 将移动到ParentViewController:要创建的视图控制器的方法 添加前作为子级添加,但它不调用 didMoveToParentViewController

我似乎找不到一个关于如何正确使用从ViewController:toViewController:duration:options:animations:completion:转换的好例子

这是正确的吗?(假设我想将一个VC与另一个VC交换)

文档中没有明确说明何时调用什么:

addChildViewController:方法调用 将移动到ParentViewController:要创建的视图控制器的方法 添加前作为子级添加,但它不调用 didMoveToParentViewController:方法。容器视图控制器 类必须调用子视图的didMoveToParentViewController: 完成到新子级的转换后的控制器,或 在调用 addChildViewController:方法

同样,这也是容器视图控制器的责任 要调用willMoveToParentViewController:方法,请在调用 从父视图控制器中移除:方法。这个 removefromparentview控制器:方法调用 didMoveToParentViewController:子视图控制器的方法


另一件事是,在这种情况下如何使用动画块?请注意,在上面的代码中,我只是将
NULL
放进去。(我对block本身很熟悉,只是不确定在这个模块中到底应该放什么)

我过去也实现过类似的功能。但是,我会将
-willMoveToParentViewController:
移动到完成块之外,因为视图控制器在移动之前应该得到通知(即,在完成块运行时,
fromVC
已经将其父VC设置为
nil
。因此,总而言之,类似这样的情况:

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC toViewController:toVC duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{} completion:^(BOOL finished) {
    [fromVC removeFromParentViewController];
    [toVC didMoveToParentViewController:self];
}];
在动画方面,根据方法文档,您不应将此参数设置为
NULL
。如果您没有要设置动画的视图属性,则只需向其传递一个空块
^{}
。基本上,此参数用于在转换期间为视图层次结构中视图的属性设置动画。可设置动画的属性列表可在“动画”下找到标题。例如,假设您不希望由
fromVC
处理的整个视图交叉分解,但只希望其视图层次结构中名为
subview1
的一个子视图淡出。您可以使用动画块执行此操作:

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC 
                  toViewController:toVC
                          duration:0.3
                           options:UIViewAnimationOptionTransitionNone
                        animations:^{
                                       [subview1 setAlpha:0.0];
                                   }
                        completion:^(BOOL finished) {
                                       [fromVC removeFromParentViewController];
                                       [toVC didMoveToParentViewController:self];
                                   }];

有意义,谢谢!您是否也在
transitionFromViewController
中实现了动画块?我更新了我的答案以解释动画块。抱歉,我第一次阅读您的问题时错过了这一点。这肯定是一种可行的方法。我自己没有尝试过,但我希望它对您有效。如果如果您对复制
UINavigationController
功能感兴趣,您可能希望将视图保存在堆栈数据结构中。您可以使用
-transitionFromViewController…
方法来实现推送和弹出方法。@zxcat抱歉,但是没有。删除操作发生在-transitionFromViewController方法中,因此-willMove应该在此之前调用,而不是在完成过程中调用。WWDC 2011会话102幻灯片显示上面写的方式是正确的。@zxcat正确,但框架希望在调用
-willMove
时视图仍在层次结构中。因此,它在
-transitionfrom
方法之前。同样,这是准确的苹果在他们的例子和文档中解释了如何做到这一点。
[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC 
                  toViewController:toVC
                          duration:0.3
                           options:UIViewAnimationOptionTransitionNone
                        animations:^{
                                       [subview1 setAlpha:0.0];
                                   }
                        completion:^(BOOL finished) {
                                       [fromVC removeFromParentViewController];
                                       [toVC didMoveToParentViewController:self];
                                   }];