Swift仿知乎tabBar隐藏显示动画

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,774评论 25 709
  • 这次的课程学习了求生存的四种姿态:讨好,指责,超理智,打岔! 主要讲解了四种姿态的冰山以及资源,学过之...
    JessieMM阅读 386评论 0 0
  • 2017年7月26日,我和女儿、孙女起了个大早,凌晨一点多起床,洗漱完毕。坐上小车向白云国际机场出发,开...
    Xiajie阅读 443评论 2 2