ios 正确使用 transitionFromViewController:toViewController:duration:options:animations:completion:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8453653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper usage of transitionFromViewController:toViewController:duration:options:animations:completion:
提问by pixelfreak
I can't seem to find a good example on how to use transitionFromViewController:toViewController:duration:options:animations:completion:
properly.
我似乎找不到关于如何transitionFromViewController:toViewController:duration:options:animations:completion:
正确使用的好例子。
Is this correct? (assuming I want to swap a VC with another)
这样对吗?(假设我想与另一个交换 VC)
// Assume fromVC and toVC view controllers are defined and fromVC is already added as a child view controller
[self addChildViewController:toVC];
[self transitionFromViewController:fromVC toViewController:toVC duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:^(BOOL finished) {
[fromVC willMoveToParentViewController:nil];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:self];
}];
The documentation isn't that clear about when to call what:
文档并不清楚何时调用什么:
The addChildViewController:method calls the willMoveToParentViewController:method of the view controller to be added as a child before adding it, but it does not call the didMoveToParentViewController:method. The container view controller class must call the didMoveToParentViewController:of the child view controller after the transition to the new child is complete or, if there is no transition, immediately after calling the addChildViewController:method.
Likewise, it is is the responsibility of the container view controller to call the willMoveToParentViewController:method before calling the removeFromParentViewController:method. The removeFromParentViewController:method calls the didMoveToParentViewController:method of the child view controller.
该addChildViewController:方法调用 willMoveToParentViewController:为孩子添加它之前被添加视图控制器的方法,但它不会调用 didMoveToParentViewController:方法。容器视图控制器类必须在过渡到新子视图完成后调用子视图控制器的didMoveToParentViewController:或者,如果没有过渡,则在调用addChildViewController:方法后立即调用 。
同样,容器视图控制器有责任在调用removeFromParentViewController:方法之前调用 willMoveToParentViewController :方法。该 removeFromParentViewController:方法调用 didMoveToParentViewController:子视图控制器的方法。
Another thing is, how do you use the animations block in this case? Notice in the above code I just put NULL
. (I am familiar with block per se, I am just not sure what to put exactly in this one)
另一件事是,在这种情况下如何使用动画块?注意在上面的代码中我只是把NULL
. (我对块本身很熟悉,我只是不确定在这个块中到底该放什么)
回答by Sean
I've implemented this sort of thing similarly in the past. But, I would move -willMoveToParentViewController:
outside the completion block since that view controller should be notified before it gets moved (i.e., by the time the completion block has run, fromVC
has already had its parent VC set to nil
. So all in all, something like this:
我过去也做过类似的事情。但是,我会移出-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];
}];
In terms of animations, you should never set this parameter to NULL
, according to the method documentation. If you have no view properties you want to animate, then you can simply pass it an empty block ^{}
. Basically this parameter is used for animating properties of your views in your view hierarchy during the transition. The list of animatable properties can be found in the UIView documentationunder the "Animations" heading. As an example, say you don't want your whole view handled by fromVC
to cross dissolve, but only want one subview in its view hierarchy named subview1
to fade out. You can do this using the animations block:
NULL
根据方法文档,在动画方面,您永远不应将此参数设置为。如果您没有想要动画的视图属性,那么您可以简单地将它传递给一个空块^{}
。基本上,此参数用于在过渡期间为视图层次结构中的视图属性设置动画。可动画属性的列表可以在UIView 文档的“动画”标题下找到。例如,假设您不希望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];
}];