transitionFromViewController只是iOS中的一种便捷方法吗?

hpi*_*que 9 iphone uiviewcontroller ios ios5

我不确定我明白究竟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];
                        }];
Run Code Online (Sandbox Code Playgroud)

...还有这个?

[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];
                 }];
Run Code Online (Sandbox Code Playgroud)

我问的原因是在某些情况下我需要将子控制器视图添加到容器控制器视图的特定子视图中.使用transitionFromViewController:toViewController:duration:options:animation:completion:不给我这个选项.

Rob*_*Rob 16

是的,我认为你是对的:它们似乎在功能上是相同的(不确定我们可以称之为方便的方法而不知道实现的细节,但很可能).显然,transitionFromViewController是专为视图控制器包含而animateWithDuration设计的,是一种通用的视图动画.

鉴于你显然正在遏制,人们可能应该使用transitionFromViewController而不是animateWithDuration.它毫不含糊,是苹果公司推荐的技术.如果你有fromViewController.view一个子视图,新的toViewController.view将被添加到同一个子视图中.

我还建议包括缺失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];
                        }];
Run Code Online (Sandbox Code Playgroud)

另请注意我正在使用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];
                        }];
Run Code Online (Sandbox Code Playgroud)

  • 在用于查看容器包含的WWDC2011视频中,Apple dev以nil为动画块传递 (3认同)