美文网首页
Swift仿知乎tabBar隐藏显示动画

Swift仿知乎tabBar隐藏显示动画

作者: 船长_ | 来源:发表于2017-01-07 23:14 被阅读297次
    demo.gif

    UIViewController单独的写了个类扩展

    使用示例

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            
            setTabBarVisible(visible: true, animated: true)
        }
        
    override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            setTabBarVisible(visible: false, animated: true)
        }
    
    extension UIViewController{
        
        func setTabBarVisible(visible: Bool, animated: Bool) {
            
            //* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
            
            // bail if the current state matches the desired state
            if tabBarIsVisible == visible { return }
            
            // get a frame calculation ready
            let frame = tabBarController?.tabBar.frame
            let height = frame?.size.height
            let offsetY = (visible ? -height! : height)
            
            // zero duration means no animation
            let duration: TimeInterval = (animated ? 0.3 : 0.0)
            
            //  animate the tabBar
            if let rect = frame {
                UIView.animate(withDuration: duration) {
                    self.tabBarController?.tabBar.frame = rect.offsetBy(dx: 0, dy: offsetY!)
                    return
                }
            }
        }
        
        var tabBarIsVisible: Bool {
            return (tabBarController?.tabBar.frame.minY)! < view.frame.maxY
        }
    }
    

    GitHub下载demo

    相关文章

      网友评论

          本文标题:Swift仿知乎tabBar隐藏显示动画

          本文链接:https://www.haomeiwen.com/subject/nzxqbttx.html