How to change the Push and Pop animations in a navigation based app

前端 未结 25 1561
清酒与你
清酒与你 2020-11-22 12:46

I have a navigation based application and I want to change the animation of the push and pop animations. How would I do that?

Edit 2018

Ther

25条回答
  •  醉话见心
    2020-11-22 13:06

    See my answer to this question for a way to do it in far fewer lines of code. This method allows you to animate a pseudo-"Push" of a new view controller any way you like, and when the animation is done it sets up the Navigation Controller just as if you had used the standard Push method. My example lets you animate either a slide-in from the left or from the right. Code repeated here for convenience:

    -(void) showVC:(UIViewController *) nextVC rightToLeft:(BOOL) rightToLeft {
        [self addChildViewController:neighbor];
        CGRect offscreenFrame = self.view.frame;
        if(rightToLeft) {
            offscreenFrame.origin.x = offscreenFrame.size.width * -1.0;
        } else if(direction == MyClimbDirectionRight) {
            offscreenFrame.origin.x = offscreenFrame.size.width;
        }
        [[neighbor view] setFrame:offscreenFrame];
        [self.view addSubview:[neighbor view]];
        [neighbor didMoveToParentViewController:self];
        [UIView animateWithDuration:0.5 animations:^{
            [[neighbor view] setFrame:self.view.frame];
        } completion:^(BOOL finished){
            [neighbor willMoveToParentViewController:nil];
            [neighbor.view removeFromSuperview];
            [neighbor removeFromParentViewController];
            [[self navigationController] pushViewController:neighbor animated:NO];
            NSMutableArray *newStack = [[[self navigationController] viewControllers] mutableCopy];
            [newStack removeObjectAtIndex:1]; //self, just below top
            [[self navigationController] setViewControllers:newStack];
        }];
    }
    

提交回复
热议问题