仿京东TabBar动画(swift)

中心思想就是继承UITabBarController,然后将系统的tabBar隐藏掉,然后创建一个View代替tabBar,然后定义好点击事件切换控制器,通过单例方法控制view的动画隐藏和显示。demo地址

调用方法

创建tabbar的时候只需要
let tabBarVC = TabBarViewController.addChildVc(controllerArr as? [Any], titles: arr as? [Any], images: defaultImageArr as? [Any], selectedImages: selectImageArr as? [Any], tabBarNaviChildVC: UINavigationController.init())       
self.window?.rootViewController = tabBarVC;
即可创建成功
动态显示隐藏是这样调用
 TabBarViewController.shareManager().hidesTabbar(yesOrNo: false, animation: true)

第一步

继承UITabBarController创建一个类TabBarViewController
1,定义一些用到的对象
let kScreenWidth:CGFloat = UIScreen.main.bounds.width
定义一个单例
static let _shareManager = TabBarViewController()   
/** 自定义的覆盖原先的tarbar的控件 */   
var tabBarView = UIView()   
/** 记录前一次选中的按钮 */ 
var selectLastBtn = TabBarButton()   
/*     * 是否隐藏tabBar     */ 
  var tabBarHidden = Bool()

第二步

 //初始化一个单例
 class func shareManager()->TabBarViewController{      return _shareManager    }  
//进入页面之后调用的方法
 override func viewDidLoad() {     
 super.viewDidLoad()     
 self.setUpTabbar()   
}   

//配置view替换tabbar 
func setUpTabbar() {     
 self.tabBar.isHidden = true       
let tabBarViewY:CGFloat = self.view.frame.size.height-49        s
elf.tabBarView = UIView.init(frame: CGRect.init(x: 0, y: tabBarViewY, width: kScreenWidth, height: 49))      
self.tabBarView.isUserInteractionEnabled = true     
self.tabBarView.backgroundColor = UIColor.white
self.tabBarView.layer.borderColor = UIColor.groupTableViewBackground.cgColor  
self.tabBarView.layer.borderWidth = 1        self.view.addSubview(self.tabBarView)    }    
//添加子控制器的方法
class func addChildVc(_ childVcs: [Any]?, titles: [Any]?, images: [Any]?, selectedImages: [Any]?, tabBarNaviChildVC: UINavigationController?) -> TabBarViewController {   
 let baseTabBar = TabBarViewController.shareManager()       
for i in 0...(childVcs?.count)! - 1 {           
let tabBarNaviChildVC:UINavigationController = UINavigationController.init(rootViewController: childVcs![i] as! UIViewController)           
baseTabBar .addChildViewController(tabBarNaviChildVC)           
baseTabBar.creatButton(normal: images?[i] as? String, selected: selectedImages?[i] as? String, tittle: titles?[i]as?String, tag: i, index: CGFloat((childVcs?.count)!))           
let button = baseTabBar.tabBarView.subviews[0];           
baseTabBar .changeViewController((button as! TabBarButton));       
}       
return baseTabBar;  
  }

// 按钮被点击时调用 

@objc func changeViewController(_ sender: TabBarButton?) {       

sender?.isEnabled = false       

if selectLastBtn != sender {           

selectLastBtn.isEnabled = true       

}        

selectLastBtn = sender!        

selectedViewController = self.viewControllers?[(sender?.tag)!]   

}

//抽出来创建按钮方法

 func creatButton(normal: String?, selected: String?, tittle: String?, tag: Int, index: CGFloat) {       

let customButton = TabBarButton(type: .custom)       

customButton.tag = tag       

let count: CGFloat = index       

let buttonW = CGFloat(tabBarView.frame.size.width / count)       

let buttonH: CGFloat = tabBarView.frame.size.height       

let rect:CGRect = CGRect(x: buttonW * CGFloat(tag), y: 0, width: buttonW, height: buttonH)       

customButton.frame = rect;       

customButton.setImage(UIImage(named: normal!), for: .normal)       

customButton.setImage(UIImage(named: selected!), for: .disabled)       

customButton.setTitle(tittle, for: .normal)       

customButton.setTitleColor(UIColor.gray, for: .normal)       

customButton.setTitleColor(UIColor.red, for: UIControlState.disabled)       

customButton.addTarget(self, action:#selector(changeViewController(_:)), for: .touchDown)        customButton.imageView?.contentMode = .center       

customButton.titleLabel?.textAlignment = NSTextAlignment.center;       

customButton.titleLabel?.font = UIFont.systemFont(ofSize: 12);       

self.tabBarView.addSubview(customButton)    }

 // 是否动画隐藏tabBar    可以使用单例方法调用

func hidesTabbar(yesOrNo:Bool,animation:Bool)  {       

self.tabBarHidden = yesOrNo;       

if yesOrNo == true {           

if tabBarView.frame.origin.y == view.frame.size.height {               

return           

}        }

else {           

if tabBarView.frame.origin.y == view.frame.size.height - 49 {               

return            }        }       

if animation == true {           

UIView.animate(withDuration: 0.3, animations: {               

if yesOrNo == true {                   

self.tabBarView.frame = CGRect(x: self.tabBarView.frame.origin.x, y: self.tabBarView.frame.origin.y + 49, width: self.tabBarView.frame.size.width, height: self.tabBarView.frame.size.height)               

} else {                   

self.tabBarView.frame = CGRect(x: self.tabBarView.frame.origin.x, y: self.tabBarView.frame.origin.y - 49, width: self.tabBarView.frame.size.width, height: self.tabBarView.frame.size.height)             

  }            })        }

else{           

UIView.animate(withDuration: 0.3, animations: {               

if yesOrNo == true {                   

self.tabBarView.frame = CGRect(x: self.tabBarView.frame.origin.x, y: self.tabBarView.frame.origin.y + 49, width: self.tabBarView.frame.size.width, height: self.tabBarView.frame.size.height)               

} else {                   

self.tabBarView.frame = CGRect(x: self.tabBarView.frame.origin.x, y: self.tabBarView.frame.origin.y - 49, width: self.tabBarView.frame.size.width, height: self.tabBarView.frame.size.height)             

  }           
})        }    }

第三步


文中的TabBarButton是继承了button,写了一个内部实现的图片在上文字在下的button。就重写了下边的两个方法

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {       

let imageW:CGFloat = contentRect.size.width;       

let imageH:CGFloat = contentRect.size.height*0.6       

return CGRect.init(x: 0, y: 5, width: imageW, height: imageH);   

}   

override func titleRect(forContentRect contentRect: CGRect) -> CGRect {       

let titleY: CGFloat = contentRect.size.height * 0.6       

let titleW: CGFloat = contentRect.size.width       

let titleH: CGFloat = contentRect.size.height - titleY       

return CGRect(x: 0, y: titleY, width: titleW, height: titleH)    }

有点瑕疵就是在侧滑返回的时候如果返回到一半的时候放弃返回,但是这时候tabbar依然会出来。可以在控制器的生命周期中控制一下

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,650评论 4 59
  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 4,667评论 0 6
  • 午后的阳光温暖的洒在身上,我陪女儿坐在地板上画画,女儿第一次尝试水粉颜料,有点小激动,简单告知水粉的使用方法后,女...
    我心自在阅读 478评论 0 3
  • 2018年5月19日 星期六 晴 女儿感冒了,有些发烧,今天带她去诊所拿了药。下午的童话剧课我征...
    周李扬阅读 150评论 0 0
  • 大学毕业后,很多同学都开始备战公务员。同寝室的一个室友,也不例外的开始了公务员的漫长之路。 阿建为人老实,酷爱学习...
    墨笔生徽阅读 2,242评论 28 32