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

前端 未结 1 1555
轻奢々
轻奢々 2020-12-04 08:06

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

Is th

相关标签:
1条回答
  • 2020-12-04 08:48

    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];
                                       }];
    
    0 讨论(0)
提交回复
热议问题