Netease-Music-Demo/wyy/MyTabBarController.swift
2021-03-06 18:45:01 +08:00

78 lines
3.0 KiB
Swift

//
// MyTabBarController.swift
// wyy
//
// Created by on 2021/3/5.
//
import UIKit
class MyTabBarController: UITabBarController {
let sideBar = SideBarView()
var sideBarWidth: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
print(view.subviews)
addChildViewControllers()
tabBar.barTintColor = UIColor.white
tabBar.isTranslucent = false
view.addSubview(sideBar)
sideBar.layoutIfNeeded()
sideBar.snp.makeConstraints{ make in
make.left.equalTo(-sideBar.frame.width)
}
}
func addChildViewControllers(){
setChildViewController(DiscoverViewController(), title: "发现", imageName: "discover", tag: 0)
setChildViewController(PodCastViewController(), title: "博客", imageName: "podcast", tag: 1)
setChildViewController(MineViewController(), title: "我的", imageName: "mine", tag: 2)
setChildViewController(KaraokeViewController(), title: "K歌", imageName: "karaoke", tag: 3)
setChildViewController(CommunityViewController(), title: "云村", imageName: "community", tag: 4)
}
func setChildViewController(_ childController: UIViewController, title: String, imageName: String, tag: Int) {
let image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
childController.tabBarItem = UITabBarItem(title: title, image: image, tag: tag)
// childController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: 0, right: 0)
childController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(openSideBar))
let navVc = UINavigationController(rootViewController: childController)
addChild(navVc)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch? = touches.first
guard let location = touch?.location(in: self.view) else { return }
print(location)
if !sideBar.frame.contains(location) {
closeSideBar()
} else {
super.touchesBegan(touches, with: event)
}
}
@objc func openSideBar(){
UIView.animate(withDuration: 0.5, animations: {
self.sideBar.frame = CGRect(x: 150, y: self.sideBar.frame.origin.y, width: self.sideBar.frame.width, height: self.sideBar.frame.height)
}, completion: { ok in
self.selectedViewController?.view.isUserInteractionEnabled = false
self.tabBar.items?.forEach{$0.isEnabled = false}
})
}
func closeSideBar(){
UIView.animate(withDuration: 0.5, animations: {
self.sideBar.frame = CGRect(x: -150, y: self.sideBar.frame.origin.y, width: self.sideBar.frame.width, height: self.sideBar.frame.height)
}, completion: { ok in
self.selectedViewController?.view.isUserInteractionEnabled = true
self.tabBar.items?.forEach{$0.isEnabled = true}
})
}
}