Jump back several steps in navigation tree with cu

2019-08-25 06:02发布

问题:

Follow up question on this one

Say my app has several ViewControllers, let's call them A,B,C,D. Usually moving back one step, i.e. from D -> C, would be using the back button in the NavigationBar or via code with the line

[self.navigationController popViewControllerAnimated:YES];

What I want to do is going back several steps, like from D -> B. This would be done with

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

Unfortunately this way I can't decide which animation to use. Instead of the default animation I have to use a Flip animation.

The only way to do this I found was 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 anyone suggest a better way to do it?

回答1:

With reference from your comments....

" sounds right, but where would I define that it's supposed to be a flip animation? As far as I can see this still uses the default animation. – taymless "

you need to perform the animation means you 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];
                                                                    }];

i hope this will help you...



回答2:

You could remove the C view controller from the navigation stack so when it pops 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 animation you want and it will pop back to view B

To do the actual flip animation when popping the view controller, 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];