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

查看:618
本文介绍了正确使用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.

同样,它是容器视图控制器的责任
在调用
removeFromParentViewController:方法之前调用 willMoveToParentViewController:方法。
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文档在动画标题下。例如,假设您不希望您的整个视图由从VC 处理以交叉解散,但只希望在其视图层次结构中有一个名为的子视图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天全站免登陆