如何禁用navigationBar动画?

发布于 2024-12-24 19:19:36 字数 862 浏览 4 评论 0原文

我有两个 UITableViewController,这样当我在第一个 UITableViewController 上单击“下一步”时,第二个 UITableViewController 就会被推送到导航堆栈上并像平常一样对过渡进行动画处理。我想做到这一点,以便当我按下下一步时,只有视图有动画,而导航栏没有(保持不变)。我已经非常接近用下面的代码来做到这一点:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CATransition *navTransition = [CATransition animation];
    navTransition.duration = .5;
    navTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    navTransition.type = kCATransitionPush;
    navTransition.subtype = kCATransitionPush;
    [self.navigationController.navigationBar.layer addAnimation:navTransition forKey:nil];

}

我放置了这段代码,并且我还制作了它,以便两个导航栏上的标题和按钮在每个 UITableViewController 中完全相同。它几乎可以工作,问题是,动画发生时导航栏会闪烁。有没有办法让它不闪烁,或者有没有其他好的方法来防止导航栏的动画发生(即禁用图层上的动画或其他东西)?

更新:有人还有其他想法吗?仍在为此苦苦挣扎。

I have two UITableViewControllers such that when I click next on the first UITableViewController, the second UITableViewController gets pushed on the navigation stack and animates the transition like normal. I'd like to make it so when I push next, only the views animate, and the navigation bar doesn't (stays the same). I've gotten very close to doing this with the code below:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CATransition *navTransition = [CATransition animation];
    navTransition.duration = .5;
    navTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    navTransition.type = kCATransitionPush;
    navTransition.subtype = kCATransitionPush;
    [self.navigationController.navigationBar.layer addAnimation:navTransition forKey:nil];

}

I put this code, and I also make it so the title and buttons on both navigation bars are exactly same in each UITableViewController. It almost works, problem is, the navigation bar blinks when the animation occurs. Is there anyway to get it to not blink, or is there any other good way to prevent the animation of the navbar from occurring (ie disabling the animation on the layer or something)?

UPDATE: Anyone got any other ideas? Still struggling with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无边思念无边月 2024-12-31 19:19:36

这就是我想出来的。以下是序列中第一个视图控制器的代码:

- (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];
                }
        }
}

这是第二个视图控制器的代码:

- (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];
                }
        }
}

您必须使用 UINavigationController 委托方法来确定 UIViewController 何时正在展示。然后,对于每个 UIViewController,需要创建一个 BOOL isInitialized 属性,以便它可以帮助您确定 UIViewController 何时被初始化推入堆栈,或者当您推回下一个 UIViewController 时显示它。

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.

谈情不如逗狗 2024-12-31 19:19:36

这可能不是最好的答案/想法,但您可以在动画期间屏蔽 UINavigationBar。

创建一个看起来与当前 UNaviationBar 完全相同的 UINavigationBar,将其添加到 keyWindow 中,在转换发生之前,然后在转换发生之后完成删除它。这基本上只覆盖 UINavigationBar 并隐藏其动画。

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.

耀眼的星火 2024-12-31 19:19:36

Swift

这是 swift 中的解决方案

var isInitialized = false

您的 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()
            }
        }
}

您的 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()
            }
        }
}

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()
            }
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文