QuoteViewController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // QuoteViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/23.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyAttributes
  10. import JXSegmentedView
  11. import WHToast
  12. /// 商品行情视图容器控制类
  13. class QuoteViewController: BaseViewController {
  14. // MARK: - 属性列表
  15. /// 分段控制器
  16. @IBOutlet weak var segmentedView: JXSegmentedView! {
  17. didSet {
  18. segmentedView.delegate = self
  19. segmentedView.dataSource = dataSource
  20. segmentedView.indicators = [self.indicator]
  21. }
  22. }
  23. /// 内容装载视图
  24. @IBOutlet weak var contentStackView: UIStackView!
  25. /// 海南网警
  26. @IBOutlet weak var police: UIButton!
  27. /// 检索按钮
  28. @IBOutlet weak var search: UIButton!
  29. /// 类目按钮
  30. @IBOutlet weak var category: UIButton!
  31. /// segmentedDataSource
  32. let dataSource: JXSegmentedTitleDataSource = {
  33. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  34. $0.titleNormalFont = .font_16
  35. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  36. $0.titleSelectedFont = .font_18
  37. $0.isTitleColorGradientEnabled = true
  38. return $0
  39. } (JXSegmentedTitleDataSource())
  40. /// 热卖商品视图
  41. lazy var hotQuoteController: HotQuoteViewController = {
  42. $0.view.frame = contentStackView.bounds
  43. return $0
  44. } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "HotQuote") as! HotQuoteViewController)
  45. // /// 新品上市视图
  46. // lazy var newQuoteController: NewQuoteViewController = {
  47. // $0.view.frame = contentStackView.bounds
  48. // return $0
  49. // } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "NewQuote") as! NewQuoteViewController)
  50. /// 特卖商城
  51. lazy var specialSaleController: SpecialSaleViewController = {
  52. $0.view.frame = contentStackView.bounds
  53. return $0
  54. } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "SpecialSale") as! SpecialSaleViewController)
  55. // MARK: - 生命周期相关
  56. required init?(coder aDecoder: NSCoder) {
  57. super.init(coder: aDecoder)
  58. /// 设置TabBar图片
  59. self.tabBarItem.image = UIImage(named: "quote_normal")?.withRenderingMode(.alwaysOriginal)
  60. self.tabBarItem.selectedImage = UIImage(named: "quote_selected")?.withRenderingMode(.alwaysOriginal)
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. // Do any additional setup after loading the view.
  65. /// 不需要返回按钮
  66. addBackBarButtonItem(true)
  67. /// UI界面显示
  68. buildView()
  69. }
  70. override func viewWillAppear(_ animated: Bool) {
  71. super.viewWillAppear(animated)
  72. /// 刷新数据
  73. dataSource.titles = ["热卖商品", "竞价商品"]
  74. segmentedView.reloadData()
  75. /// 隐藏导航栏
  76. self.navigationController?.setNavigationBarHidden(true, animated: true)
  77. }
  78. // MARK: - 初始化
  79. /// UI界面显示
  80. fileprivate func buildView() {
  81. showController(selectedIndex: 0)
  82. }
  83. // MARK: - 交互相关
  84. @IBAction func onButtonPressed(_ sender: UIButton) {
  85. switch sender {
  86. case police: /// 海南网警
  87. push(storyboardName: "Feedback", storyboardId: "Feedback")
  88. case category: /// 类目
  89. let category = viewController(name: "Category", identifier: "CategoryID")
  90. self.navigationController?.pushViewController(category, animated: true)
  91. default: break
  92. }
  93. }
  94. // MARK: - UI显示
  95. /// Tab也切换时显示的容器
  96. /// - Parameter index: index
  97. fileprivate func showController(selectedIndex index: Int) {
  98. switch index {
  99. case 0: /// 热卖商品
  100. /// 移出新品上市视图
  101. removeChildren(hotQuoteController)
  102. // /// 移出新品上市视图
  103. // removeChildren(newQuoteController)
  104. /// 添加特卖商城视图
  105. addChildren(specialSaleController)
  106. // case 1:
  107. // /// 移出新品上市视图
  108. // removeChildren(hotQuoteController)
  109. // /// 移出新品上市视图
  110. // removeChildren(specialSaleController)
  111. // /// 添加特卖商城视图
  112. // addChildren(newQuoteController)
  113. default: /// 竞价商品
  114. // /// 移出新品上市视图
  115. // removeChildren(newQuoteController)
  116. /// 移出新品上市视图
  117. removeChildren(specialSaleController)
  118. /// 添加特卖商城视图
  119. addChildren(hotQuoteController)
  120. }
  121. }
  122. /// addChildren
  123. /// - Parameter child: child
  124. fileprivate func addChildren(_ child: UIViewController) {
  125. if !contentStackView.arrangedSubviews.contains(child.view) {
  126. self.addChild(child)
  127. child.didMove(toParent: self)
  128. contentStackView.addArrangedSubview(child.view)
  129. }
  130. }
  131. /// removeChildren
  132. /// - Parameter child: child
  133. fileprivate func removeChildren(_ child: UIViewController) {
  134. if contentStackView.arrangedSubviews.contains(child.view) {
  135. child.removeFromParent()
  136. contentStackView.removeArrangedSubview(child.view)
  137. child.view.removeFromSuperview()
  138. }
  139. }
  140. // MARK: - Navigation
  141. // In a storyboard-based application, you will often want to do a little preparation before navigation
  142. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  143. // Get the new view controller using segue.destination.
  144. // Pass the selected object to the new view controller.
  145. }
  146. }
  147. extension QuoteViewController: JXSegmentedViewDelegate {
  148. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  149. /// 显示相应的视图
  150. self.showController(selectedIndex: index)
  151. /// 隐藏分类按钮
  152. self.category.isHidden = !(index == 0)
  153. }
  154. }