Proper usage of transitionFromViewController:toViewController:duration:options:animations:completion: Proper usage of transitionFromViewController:toViewController:duration:options:animations:completion: ios ios

Proper usage of transitionFromViewController:toViewController:duration:options:animations:completion:


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];}];

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];                                   }];


matomo