Netease-Music-Demo by sss
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyTabBarController.swift 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // MyTabBarController.swift
  3. // wyy
  4. //
  5. // Created by 施书顺 on 2021/3/5.
  6. //
  7. import UIKit
  8. class MyTabBarController: UITabBarController {
  9. let sideBar = SideBarView()
  10. var sideBarWidth: CGFloat = 0
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. print(view.subviews)
  14. addChildViewControllers()
  15. tabBar.barTintColor = UIColor.white
  16. tabBar.isTranslucent = false
  17. view.addSubview(sideBar)
  18. sideBar.layoutIfNeeded()
  19. sideBar.snp.makeConstraints{ make in
  20. make.left.equalTo(-sideBar.frame.width)
  21. }
  22. }
  23. func addChildViewControllers(){
  24. setChildViewController(DiscoverViewController(), title: "发现", imageName: "discover", tag: 0)
  25. setChildViewController(PodCastViewController(), title: "博客", imageName: "podcast", tag: 1)
  26. setChildViewController(MineViewController(), title: "我的", imageName: "mine", tag: 2)
  27. setChildViewController(KaraokeViewController(), title: "K歌", imageName: "karaoke", tag: 3)
  28. setChildViewController(CommunityViewController(), title: "云村", imageName: "community", tag: 4)
  29. }
  30. func setChildViewController(_ childController: UIViewController, title: String, imageName: String, tag: Int) {
  31. let image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
  32. childController.tabBarItem = UITabBarItem(title: title, image: image, tag: tag)
  33. // childController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: 0, right: 0)
  34. childController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(openSideBar))
  35. let navVc = UINavigationController(rootViewController: childController)
  36. addChild(navVc)
  37. }
  38. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  39. let touch: UITouch? = touches.first
  40. guard let location = touch?.location(in: self.view) else { return }
  41. print(location)
  42. if !sideBar.frame.contains(location) {
  43. closeSideBar()
  44. } else {
  45. super.touchesBegan(touches, with: event)
  46. }
  47. }
  48. @objc func openSideBar(){
  49. UIView.animate(withDuration: 0.5, animations: {
  50. self.sideBar.frame = CGRect(x: 150, y: self.sideBar.frame.origin.y, width: self.sideBar.frame.width, height: self.sideBar.frame.height)
  51. }, completion: { ok in
  52. self.selectedViewController?.view.isUserInteractionEnabled = false
  53. self.tabBar.items?.forEach{$0.isEnabled = false}
  54. })
  55. }
  56. func closeSideBar(){
  57. UIView.animate(withDuration: 0.5, animations: {
  58. self.sideBar.frame = CGRect(x: -150, y: self.sideBar.frame.origin.y, width: self.sideBar.frame.width, height: self.sideBar.frame.height)
  59. }, completion: { ok in
  60. self.selectedViewController?.view.isUserInteractionEnabled = true
  61. self.tabBar.items?.forEach{$0.isEnabled = true}
  62. })
  63. }
  64. }