【问题标题】:Navigation Bar Blinks When Animating for the First Time首次制作动画时导航栏闪烁
【发布时间】:2014-11-17 20:27:44
【问题描述】:

我已经创建了自己的各种 tabbarcontroller,但在动画方面遇到了问题。当我在选项卡单击时为视图设置动画时,导航栏会完全变黑(应该是红色),然后在动画完成后闪烁回红色。我的设置和代码如下。

(在 swift 或 Objective-c 中的答案都很有帮助,因为翻译很简单)

提前致谢!

红色:导航栏

蓝色:导航显示视图

灰色:标签栏

酒红色:标签栏显示视图(这是正在转换/动画的部分)

我使用以下代码在我的视图之间转换/动画。

 //Handles selection of a tab bar item
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {

        //Get controller for transition
        var selectedController = self.controllers[item.tag];

        //Only transition for new view
        if(self.childViewControllers[0] as UIViewController != selectedController!){
            //Prepare for controller transition
            selectedController!.viewWillAppear(false)
            self.currViewController?.viewWillDisappear(false)
            self.currViewController?.willMoveToParentViewController(nil)

            //Set new controller with animation
            selectedController!.view.frame = self.displayView.frame
            if(transitionAnimated){
                UIView.transitionFromView(self.displayView, toView: selectedController!.view, duration: self.animationDuration, options: self.animationOptions, completion: {finished in
                        self.setNewController(selectedController!)
                        self.animationCompletion(finished)
                })
            }
            else{
                self.displayView.removeFromSuperview()
                self.view.addSubview(selectedController!.view)
                setNewController(selectedController!);
            }
        }
        else{
            if(self.childViewControllers[0].isKindOfClass(UINavigationController)){
                (self.childViewControllers[0] as UINavigationController).popToRootViewControllerAnimated(true)
            }
        }
    }

【问题讨论】:

  • 您是否考虑过使用UIViewControllerAnimatedTransitioning 自定义标签栏控制器动画? developer.apple.com/library/ios/documentation/uikit/reference/…。没关系,我刚刚意识到您制作了自己的标签栏控制器,所以上面的内容可能对您没有帮助。
  • 我打赌你没有在正确的时间发送所有正确的转换消息。为什么不只是use transitionFromViewController:toViewController:duration:options:animations:completion:
  • @AaronBrager 当我使用 transitionFromViewController 时,视图不是我的 displayView 的子视图。我的 displayView 缩小和增长取决于选项卡栏是隐藏还是显示(有一些实用方法可以为选项卡栏的框架和 displayView 的框架设置动画)。使用 transitionFromViewController 时生成的视图控制器在显示时覆盖标签栏。如果您可以为此提供解决方案,那么我就是游戏。

标签: objective-c swift uinavigationcontroller uitabbar uiviewanimation


【解决方案1】:

我在 UINavigationController 上遇到了类似的问题,这解决了它,试试吧:

就在这一行之前:

UIView.transitionFromView(self.displayView, toView: selectedController!.view, duration: self.animationDuration, options: self.animationOptions, completion: {finished in

像这样将 selectedController.view 添加到视图层次结构中(对不起 Obj-C 代码) [self.displayView.superview addSubview:selectedController.view];

让我知道它是否有效 :) 祝你好运!

【讨论】:

  • 那行得通。我不知道为什么,但确实如此。你能解释一下为什么这会停止闪烁的导航栏吗?
  • 我相信(我不确定这只是一个假设)问题在于过渡正在使用视图层(presentationLayer)的快照并且视图本身没有添加到视图层次结构,并且由于仅当视图实际添加到视图层次结构时才会呈现导航栏,因此它可以访问“外观代理”设置,因此它不会在过渡开始时呈现。这是我最初的假设,我的测试成功了,但这并不意味着我的假设没问题:(
  • 哇 - 我已经尝试了很多变体来使这项工作没有闪烁的导航栏,但在此之前没有任何工作。我也不明白,但谢谢你找到它!
【解决方案2】:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        guard let fromView = selectedViewController?.view, let toView = viewController.view else
        {
            return false
        }

        if fromView != toView
        {
            fromView.superview!.addSubview(toView)
            UIView.transition(from: fromView, to: toView, duration: 0.15, options: UIView.AnimationOptions.transitionCrossDissolve, completion: nil)
        }

        return true
    }

这对我有用!!!

【讨论】:

  • 要改进您的答案,请添加更多代码的 cmets/描述。
【解决方案3】:

对我来说,这是因为我在标签栏后面有一个视图。当我在动画时隐藏视图时,它解决了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多