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

前端 未结 25 1103
清酒与你
清酒与你 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:19

    From the sample app, check out this variation. https://github.com/mpospese/MPFoldTransition/

    #pragma mark - UINavigationController(MPFoldTransition)
    
    @implementation UINavigationController(MPFoldTransition)
    
    //- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    - (void)pushViewController:(UIViewController *)viewController foldStyle:(MPFoldStyle)style
    {
        [MPFoldTransition transitionFromViewController:[self visibleViewController] 
                                      toViewController:viewController 
                                              duration:[MPFoldTransition defaultDuration]  
                                                 style:style 
                                            completion:^(BOOL finished) {
                                                [self pushViewController:viewController animated:NO];
                                            }
         ];
    }
    
    - (UIViewController *)popViewControllerWithFoldStyle:(MPFoldStyle)style
    {
        UIViewController *toController = [[self viewControllers] objectAtIndex:[[self viewControllers] count] - 2];
    
        [MPFoldTransition transitionFromViewController:[self visibleViewController] 
                                      toViewController:toController 
                                              duration:[MPFoldTransition defaultDuration] 
                                                 style:style
                                            completion:^(BOOL finished) {
                                                [self popViewControllerAnimated:NO];
                                            }
         ];
    
        return toController;
    }
    
    0 讨论(0)
  • 2020-11-22 13:20

    Remember that in Swift, extension are definitely your friends!

    public extension UINavigationController {
    
        /**
         Pop current view controller to previous view controller.
    
         - parameter type:     transition animation type.
         - parameter duration: transition animation duration.
         */
        func pop(transitionType type: String = kCATransitionFade, duration: CFTimeInterval = 0.3) {
            self.addTransition(transitionType: type, duration: duration)
            self.popViewControllerAnimated(false)
        }
    
        /**
         Push a new view controller on the view controllers's stack.
    
         - parameter vc:       view controller to push.
         - parameter type:     transition animation type.
         - parameter duration: transition animation duration.
         */
        func push(viewController vc: UIViewController, transitionType type: String = kCATransitionFade, duration: CFTimeInterval = 0.3) {
            self.addTransition(transitionType: type, duration: duration)
            self.pushViewController(vc, animated: false)
        }
    
        private func addTransition(transitionType type: String = kCATransitionFade, duration: CFTimeInterval = 0.3) {
            let transition = CATransition()
            transition.duration = duration
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = type
            self.view.layer.addAnimation(transition, forKey: nil)
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 13:21

    for push

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    //transition.subtype = kCATransitionFromTop;
    
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.navigationController pushViewController:ViewControllerYouWantToPush animated:NO];
    

    for pop

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    //transition.subtype = kCATransitionFromTop;
    
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.navigationController popViewControllerAnimated:NO];
    
    0 讨论(0)
  • 2020-11-22 13:21

    Based on jordanperry answer updated for swift 4

    For push UIViewController

    let yourVC = self.storyboard?.instantiateViewController(withIdentifier: "yourViewController") as! yourViewController
        UIView.animate(withDuration: 0.75, animations: {() -> Void in
        UIView.setAnimationCurve(.easeInOut)
        self.navigationController?.pushViewController(terms, animated: true)
        UIView.setAnimationTransition(.flipFromRight, for: (self.navigationController?.view)!, cache: false)
    })
    

    For Pop

    UIView.animate(withDuration: 0.75, animations: {() -> Void in
        UIView.setAnimationCurve(.easeInOut)
        UIView.setAnimationTransition(.flipFromLeft, for: (self.navigationController?.view)!, cache: false)
    })
    navigationController?.popViewController(animated: false)
    
    0 讨论(0)
  • 2020-11-22 13:21

    While all the answers here are great and most work very well, there is a slightly simpler method which achieves the same effect...

    For Push:

      NextViewController *nextViewController = [[NextViewController alloc] init];
    
      // Shift the view to take the status bar into account 
      CGRect frame = nextViewController.view.frame;
      frame.origin.y -= 20;
      frame.size.height += 20;
      nextViewController.view.frame = frame;
    
      [UIView transitionFromView:self.navigationController.topViewController.view toView:nextViewController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
        [self.navigationController pushViewController:nextViewController animated:NO];
      }];
    

    For Pop:

      int numViewControllers = self.navigationController.viewControllers.count;
      UIView *nextView = [[self.navigationController.viewControllers objectAtIndex:numViewControllers - 2] view];
    
      [UIView transitionFromView:self.navigationController.topViewController.view toView:nextView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        [self.navigationController popViewControllerAnimated:NO];
      }];}
    
    0 讨论(0)
  • 2020-11-22 13:24

    Have a look at ADTransitionController, a drop in replacement for UINavigationController with custom transition animations (its API matches the API of UINavigationController) that we created at Applidium.

    You can use different pre-defined animations for push and pop actions such as Swipe, Fade, Cube, Carrousel, Zoom and so on.

    0 讨论(0)
提交回复
热议问题