Skip to content

Commit

Permalink
* 更改页面数据数据传递
Browse files Browse the repository at this point in the history
  • Loading branch information
Caffe-del committed Sep 20, 2020
1 parent bbe67b2 commit f0cf350
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 75 deletions.
2 changes: 1 addition & 1 deletion BottomBar/AccountViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AccountViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .systemGray
self.view.backgroundColor = UIColor.init(red: 104/255, green: 237/255, blue: 198/255, alpha: 1)
// Do any additional setup after loading the view.
}

Expand Down
21 changes: 21 additions & 0 deletions BottomBar/Assets.xcassets/Normal/minibar_bg.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion BottomBar/BBSViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BBSViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .systemBlue
self.view.backgroundColor = UIColor.init(red: 144/255, green: 190/255, blue: 222/255, alpha: 1)
// Do any additional setup after loading the view.
}

Expand Down
2 changes: 1 addition & 1 deletion BottomBar/MusicDetailVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MusicDetailVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .systemBlue;
self.view.backgroundColor = UIColor.init(red: 255/255, green: 228/255, blue: 181/255, alpha: 1)
// Do any additional setup after loading the view.
}

Expand Down
4 changes: 2 additions & 2 deletions BottomBar/MusicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MusicViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.backgroundColor = UIColor.init(red: 229/255, green: 225/255, blue: 238/255, alpha: 1)
self.navigationController?.delegate = self

let switchBtn = UISwitch.init(frame: CGRect.init(origin: self.view.center, size: CGSize.init(width: 60, height: 50)))
Expand All @@ -36,7 +36,7 @@ class MusicViewController: UIViewController {
sharedGloble.isSkinStyle = switchBtn.isOn

if let tabBarController = self.tabBarController as? RootTabBarController {
tabBarController.updateItemsInfo()
tabBarController.updateRootTabBarInfos()
tabBarController.updateBottomStyle(.all)
}
}
Expand Down
94 changes: 62 additions & 32 deletions BottomBar/RootTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,77 @@ protocol MainTabBarDelegate: NSObjectProtocol {
class RootTabBar: UITabBar {

var bottomBarStyle = BottomBarStyle.miniPlayViewOnly
var itemsInfo: [itemInfoTuple]!
var bgImg: UIImage!
var RTDelegate: MainTabBarDelegate?
var tabBarItems = [RootTabBarItem]() {
didSet {
for barItem in tabBarItems {
itemsContainer!.addSubview(barItem)
barItem.RTDelegate = self
}
}

private var tabBarItems = [RootTabBarItem]()
private var bgImgView: UIImageView! // 背景图
private var miniPlayerView: UIView! // mini播放条
private var itemsContainer: UIView! // 装载items的容器

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .clear;
self.backgroundImage = UIImage.init()
self.shadowImage = UIImage.init()

self.initSubviews()
}

private lazy var itemsContainer: UIView! = {
let itemsContainer = UIView.init()
self.addSubview(itemsContainer)
return itemsContainer
}()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private lazy var bgImgView: UIImageView! = {
let bgImgView = UIImageView.init(image: UIImage.init(named: "skin_minibar_bg"))
func initSubviews() {
bgImgView = UIImageView.init(image: UIImage.init(named: "skin_minibar_bg"))
self.addSubview(bgImgView)
self.sendSubviewToBack(bgImgView)
return bgImgView
}()

private lazy var miniPlayerView: UIView! = {
let miniPlayerView = UIView.init()

miniPlayerView = UIView.init()
miniPlayerView.backgroundColor = .systemTeal
miniPlayerView.layer.cornerRadius = miniPlayViewHeight * 0.5
miniPlayerView.layer.masksToBounds = true
self.addSubview(miniPlayerView)
return miniPlayerView
}()

itemsContainer = UIView.init()
self.addSubview(itemsContainer)
}

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .clear;
self.backgroundImage = UIImage.init()
self.shadowImage = UIImage.init()
func createTabBarItems() {
for (index, _) in itemsInfo.enumerated() {
let tabBarItem = RootTabBarItem()
tabBarItem.tag = index
itemsContainer!.addSubview(tabBarItem)
tabBarItems.append(tabBarItem)
tabBarItem.RTDelegate = self
}
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
// 刷新数据
func reloadData(_ selectedIdx:Int) {
bgImgView.image = bgImg
for (index, item) in tabBarItems.enumerated() {
let info = itemsInfo[index]
item.iconName = info.name
item.iconSelectedName = info.selectName

item.titleColor = info.color
item.titleSelectedColor = info.selectColor

item.title = info.title
}

self.updateBarItems(selectedIdx: selectedIdx)
}

/// 更新items 的状态
/// - Parameter selected: 当前选中的索引
func updateBarItems(selectedIdx:Int) {
for (idx,item) in tabBarItems.enumerated() {
let state:itemState = selectedIdx==idx ? .selected : .normal
item.reloadData(state: state)
}
}

override func layoutSubviews() {
Expand All @@ -79,12 +109,12 @@ class RootTabBar: UITabBar {
let viewX = (screenWidth - viewW) / 2
miniPlayerView.frame = CGRect.init(x: viewX, y: 0, width: viewW, height: miniPlayViewHeight)

// TabBatItems
itemsContainer.frame = CGRect.init(x: 0, y: miniPlayViewHeight, width: screenWidth, height: tabBarHeight)
// TabBarItems
let layoutY = bottomBarStyle == .miniPlayViewOnly ? miniPlayViewHeight + safeAreaBottomHeight : miniPlayViewHeight
itemsContainer.frame = CGRect.init(x: 0, y: layoutY, width: screenWidth, height: tabBarHeight)
let itemW = screenWidth / CGFloat(tabBarItems.count)
for (index,barItem) in tabBarItems.enumerated() {
let layoutY = bottomBarStyle == .miniPlayViewOnly ? safeAreaBottomHeight : 0
barItem.frame = CGRect.init(x: itemW*CGFloat(index), y: layoutY, width: itemW, height: tabBarHeight)
barItem.frame = CGRect.init(x: itemW*CGFloat(index), y: 0, width: itemW, height: tabBarHeight)
}
}
}
Expand Down
57 changes: 21 additions & 36 deletions BottomBar/RootTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ enum BottomBarStyle {
case none
}

class RootTabBarController: UITabBarController {
typealias itemInfoTuple = (name:String, selectName:String,color:UIColor, selectColor:UIColor, title:String)

private typealias itemInfoTuple = (name:String, selectName:String,color:UIColor, selectColor:UIColor, title:String)
class RootTabBarController: UITabBarController {

private var rootTabBar: RootTabBar!
private var bottomBarStyle = BottomBarStyle.miniPlayViewOnly
private var itemList = [RootTabBarItem]()

private var tabBarBgImg: UIImage {
get {
let imgStr = sharedGloble.isSkinStyle ? "skin_minibar_bg":"minibar_bg"
let tabBarBgImg = UIImage.init(named: imgStr)!
return tabBarBgImg
}
}

private var itemsInfo: [itemInfoTuple] {
get {
var itemsInfo: [itemInfoTuple]
Expand All @@ -45,14 +52,9 @@ class RootTabBarController: UITabBarController {

rootTabBar = RootTabBar()
rootTabBar.RTDelegate = self

for (index, _) in itemsInfo.enumerated() {
let tabBatItem = RootTabBarItem()
tabBatItem.tag = index
itemList.append(tabBatItem)
}

rootTabBar.tabBarItems = itemList
rootTabBar.itemsInfo = itemsInfo
rootTabBar.bgImg = tabBarBgImg
rootTabBar.createTabBarItems()
self.setValue(rootTabBar, forKey: "tabBar")

let musicVC = MusicViewController()
Expand All @@ -69,7 +71,7 @@ class RootTabBarController: UITabBarController {

self.viewControllers = [musicNav, videoNav, BBSNav, accountNav];

self.updateItemsInfo()
self.updateRootTabBarInfos()
}

override func viewDidLayoutSubviews() {
Expand All @@ -83,25 +85,17 @@ class RootTabBarController: UITabBarController {
case .all:
tabBarY = screenHeight - tabBarHeight - miniPlayViewHeight - safeAreaBottomHeight
case .miniPlayViewOnly:
tabBarY = screenHeight - tabBarHeight - safeAreaBottomHeight
tabBarY = screenHeight - miniPlayViewHeight - safeAreaBottomHeight
}

self.tabBar.frame = CGRect.init(x: 0, y: tabBarY, width: screenWidth, height: tabBarHeight + miniPlayViewHeight + safeAreaBottomHeight)
}

/// 更新items 所需要的信息,并且刷新展示
func updateItemsInfo() {
for (index, item) in itemList.enumerated() {
let info = itemsInfo[index]
item.iconName = info.name
item.iconSelectedName = info.selectName

item.titleColor = info.color
item.titleSelectedColor = info.selectColor

item.title = info.title
}
self.updateBarItems(selected: self.selectedIndex)
/// 更新RotTabBar所需要的信息,并且刷新展示
func updateRootTabBarInfos() {
rootTabBar.itemsInfo = itemsInfo
rootTabBar.bgImg = tabBarBgImg
rootTabBar.reloadData(self.selectedIndex)
}

/// 更新底部的样式
Expand All @@ -116,21 +110,12 @@ class RootTabBarController: UITabBarController {
}, completion: nil)
}

/// 更新items 的状态
/// - Parameter selected: 当前选中的索引
func updateBarItems(selected:Int) {
for (idx,item) in itemList.enumerated() {
let state:itemState = selected==idx ? .selected : .normal
item.reloadData(state: state)
}
}

}

extension RootTabBarController: MainTabBarDelegate {
func tabBar(_ tabBar: RootTabBar, didSelectedIndex: Int) {
self.selectedIndex = didSelectedIndex;
self.updateBarItems(selected: didSelectedIndex)
rootTabBar.reloadData(self.selectedIndex)
}

}
2 changes: 1 addition & 1 deletion BottomBar/VideoDetailVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class VideoDetailVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .systemPink
self.view.backgroundColor = UIColor.init(red: 255/255, green: 228/255, blue: 181/255, alpha: 1)
// Do any additional setup after loading the view.
}

Expand Down
2 changes: 1 addition & 1 deletion BottomBar/VideoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class VideoViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .systemRed
self.view.backgroundColor = UIColor.init(red: 223/255, green: 253/255, blue: 255/255, alpha: 1)
// Do any additional setup after loading the view.
}

Expand Down

0 comments on commit f0cf350

Please sign in to comment.