如何禁用navigationBar动画?
我有两个UITableViewControllers,当我在第一个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中完全相同。 它几乎可以工作,问题是,动画发生时导航栏会闪烁。 反正是为了让它不闪烁,还是有任何其他好办法阻止导航栏的动画发生(即禁用图层上的动画或其他东西)?
更新:任何人有任何其他想法? 仍然在努力解决这个问题。
这就是我提出的。 这是序列中第一个viewController的代码:
- (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
。
这可能不是最好的答案/想法,但您可以在动画期间屏蔽UINavigationBar。
创建一个UINavigationBar
,其外观与当前UNavigationBar
在转换发生之前将其添加到keyWindow
方式完全相同,然后在完成后将其删除。 这基本上只涵盖UINavigationBar
并隐藏其动画。
迅速
这是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() } } }