TransitionFromViewController

TransitionFromViewController

我不确定 transitionFromViewController:toViewController:duration:options:animation:completion: 到底能做什么。这只是一种方便的方法吗?

例如,这样做之间有什么区别...

[self transitionFromViewController:fromViewController
                  toViewController:toViewController
                          duration:0.25
                           options:UIViewAnimationOptionCurveEaseInOut
                        animations:^{
                            fromViewController.view.alpha = 0;
                            toViewController.view.alpha = 1;
                        } completion:^(BOOL finished) {
                            [fromViewController removeFromParentViewController];
                            [toViewController didMoveToParentViewController:self];
                        }];

...和这个?
[self.view addSubview:toViewController.view];
[UIView animateWithDuration:0.25
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     fromViewController.view.alpha = 0;
                     toViewController.view.alpha = 1;
                 } completion:^(BOOL finished){
                     [fromViewController.view removeFromSuperview];
                     [fromViewController removeFromParentViewController];
                     [toViewController didMoveToParentViewController:self];
                 }];

我问的原因是,在某些情况下,我需要将子 Controller View 添加到容器 Controller View 的特定 subview 中。使用transitionFromViewController:toViewController:duration:options:animation:completion:不会给我这个选项。

最佳答案

是的,我认为您是对的:它们在功能上似乎是相同的(不确定我们是否可以在不知道实现细节的情况下将其称为便捷方法,但很可能是正确的)。显然,transitionFromViewController设计用于 View Controller 包含,而animateWithDuration设计用于 View 的通用动画。

鉴于您显然正在进行遏制,因此可能应该使用transitionFromViewController而不是animateWithDuration。它是明确的,是recommended by Apple技术。如果 subview 中有fromViewController.view,则新的toViewController.view将添加到同一 subview 中。

我还建议包括缺少的willMoveToParentViewControlleraddChildViewController(为简洁起见,我假设您将其省略,但出于完整性考虑,我将其包括在内):

[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];

[self transitionFromViewController:fromViewController
                  toViewController:toViewController
                          duration:0.25
                           options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{}
                        completion:^(BOOL finished){
                            [fromViewController removeFromParentViewController];
                            [toViewController didMoveToParentViewController:self];
                        }];

另请注意,我正在使用UIViewAnimationOptionTransitionCrossDissolve。如果您是手动设置Alpha,请不要忘记初始化toViewController.view.alpha,例如:
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
toViewController.view.alpha = 0.0;

[self transitionFromViewController:fromViewController
                  toViewController:toViewController
                          duration:0.25
                           options:UIViewAnimationOptionCurveEaseInOut
                        animations:{
                            fromViewController.view.alpha = 0.0;
                            toViewController.view.alpha = 1.0;
                        }
                        completion:^(BOOL finished){
                            [fromViewController removeFromParentViewController];
                            [toViewController didMoveToParentViewController:self];
                        }];

10-02 14:35