正确使用 transitionFromViewController:toViewController:duration:options:animations:completion: [英] Proper usage of transitionFromViewController:toViewController:duration:options:animations:completion:

查看:23
本文介绍了正确使用 transitionFromViewController:toViewController:duration:options:animations:completion:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到关于如何正确使用 transitionFromViewController:toViewController:duration:options:animations:completion: 的好例子.

I can't seem to find a good example on how to use transitionFromViewController:toViewController:duration:options:animations:completion: properly.

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

Is this correct? (assuming I want to swap a VC with another)

// 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:

addChildViewController: 方法调用willMoveToParentViewController: 视图控制器的方法在添加之前作为子添加,但它不会调用didMoveToParentViewController: 方法.容器视图控制器类必须调用子视图的 didMoveToParentViewController:过渡到新孩子完成后的控制器,或者,如果没有转换,在调用之后立即addChildViewController: 方法.

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.

同样,它是容器视图控制器的责任在调用之前调用 willMoveToParentViewController: 方法removeFromParentViewController: 方法.这removeFromParentViewController: 方法调用didMoveToParentViewController:子视图控制器的方法.

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.

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

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)

推荐答案

我过去也做过类似的事情.但是,我会将 -willMoveToParentViewController: 移动到完成块之外,因为应该在移动之前通知该视图控制器(即,当完成块运行时,fromVC已经将其父 VC 设置为 nil.所以总而言之,就像这样:

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:

[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.如果您没有想要动画的视图属性,那么您可以简单地向它传递一个空块 ^{}.基本上,此参数用于在过渡期间为视图层次结构中的视图属性设置动画.可动画属性列表可以在 UIView 文档在动画"标题下.例如,假设您不希望 fromVC 处理的整个视图交叉融合,而只希望其视图层次结构中名为 subview1 的子视图淡出.你可以使用动画块来做到这一点:

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 documentation under 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:

[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:toViewController:duration:options:animations:completion:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆