How to disable navigationBar animation?

随声附和 提交于 2019-12-03 21:23:34

This is what I came up with. Here is the code for the first viewController in the sequence:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        if (viewController == self)
        {
                if (self.isInitialized)
                {
                        CATransition *navigationBarAnimation = [CATransition animation];
                        navigationBarAnimation.duration = 1.5;
                        navigationBarAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
                        navigationBarAnimation.type = kCATransitionFade;
                        navigationBarAnimation.subtype = kCATransitionFade;
                        navigationBarAnimation.removedOnCompletion = YES;
                        [self.navigationController.navigationBar.layer addAnimation:navigationBarAnimation forKey:nil];
                }
                else 
                {
                        self.isInitialized = YES;
                }
        }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        if (viewController == self)
        {
                if (self.isInitialized)
                {
                        [self.navigationController.navigationBar.layer removeAllAnimations];
                }
        }
}

Here is the code for the 2nd view controller:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        if (viewController == self)
        {
                if (!self.isInitialized)
                {
                        CATransition *navigationBarAnimation = [CATransition animation];
                        navigationBarAnimation.duration = 1.5;
                        navigationBarAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
                        navigationBarAnimation.type = kCATransitionFade;
                        navigationBarAnimation.subtype = kCATransitionFade;
                        navigationBarAnimation.removedOnCompletion = YES;
                        [self.navigationController.navigationBar.layer addAnimation:navigationBarAnimation forKey:nil];
                        self.isInitialized = YES;
                }
        }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        if (viewController == self)
        {
                if (self.isInitialized)
                {
                        [self.navigationController.navigationBar.layer removeAllAnimations];
                }
        }
}

You have to use the UINavigationController delegate methods to figure out when the UIViewController is being shown. Then for each UIViewController, need to make a BOOL isInitialized property so it helps you determine when the UIViewController is being pushed on the stack, or when it's being shown because you pushed back on the next UIViewController.

This may not be the best answer/idea but you could just mask the UINavigationBar during the animation.

Create a UINavigationBar that looks exactly the same way as your current UNavigationBar add it to the keyWindow just before the transition takes place, then after it is finished remove it. This will essentially cover just the UINavigationBar and hide its animation.

Swift

Here is solution in swift

var isInitialized = false

Your FirstViewController:

func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                var navigationBarAnimation = CATransition()
                navigationBarAnimation.duration = 1.5
                navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                navigationBarAnimation.type = kCATransitionFade
                navigationBarAnimation.subtype = kCATransitionFade
                navigationBarAnimation.removedOnCompletion = true
                self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
                }
                else 
                {
                        self.isInitialized = true;
                }
        }
}

func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                self.navigationController?.navigationBar?.layer?.removeAllAnimations()
            }
        }
}

Your SecondViewController:

func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if !self.isInitialized {
                var navigationBarAnimation = CATransition()
                navigationBarAnimation.duration = 1.5
                navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                navigationBarAnimation.type = kCATransitionFade
                navigationBarAnimation.subtype = kCATransitionFade
                navigationBarAnimation.removedOnCompletion = true
                self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
                        self.isInitialized = true;
                }
        }
}

func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                self.navigationController?.navigationBar?.layer?.removeAllAnimations()
            }
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!