Go back a few steps in the custom animation navigation tree

Follow-up question about this

Say that my application has several ViewControllers , call them A, B, C, D. Usually go back one step, i.e. From D → C, will use the return button in the NavigationBar or through code with a line

 [self.navigationController popViewControllerAnimated:YES]; 

I want to take several steps, for example, from D → B. This will be done with

 [self.navigationController popToViewController:B animated:YES]; 

Unfortunately, this way I cannot decide which animation to use. Instead of the default animation, I need to use Flip animation.

The only way to do this, I found this method

 [self.navigationController transitionFromViewController:self.navigationController.presentedViewController toViewController:B duration:300 options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil]; 

Is this the only way to achieve this, or can offer a better way to do this?

0
source share
2 answers

With reference to your comments ....

"sounds correct, but where would I determine that it should be flip animation? As far as I can see, this still uses the default animation. - taymless"

you need to perform the animation to use this block of code ....

  -(void)animatefromView:(UIView*)fromView toBlock:(void (^)(void))block // note block may be any executable code... { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration: 1]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:fromView cache:NO]; block(); [UIView commitAnimations]; } 

and the method call will be ....

 [self animatefromView:self.navigationController.view toBlock:^{ [self.navigationController pushViewController:ani animated:NO]; }]; 

Hope this helps you ...

0
source

You can remove the view controller C from the navigation stack, so when it appears from D, it will go directly to view B

 // Remove Controller C from Stack NSMutableArray *controllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]]; [controllers removeObject:C]; [self.navigationController setViewControllers:controllers]; 

Then you can use [self.navigationController popViewControllerAnimated:YES]; with the desired animation and it will appear again to view B

To do the actual flip animation when the view controller appears, you can use the animation code below

 [UIView beginAnimations:@"flip_animation" context:nil]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromTop forView:self.navigationController.view cache:NO]; [self.navigationController popViewControllerAnimated:NO]; [UIView commitAnimations]; 
+1
source

Source: https://habr.com/ru/post/920018/


All Articles