// // QuoteViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/10/23. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import SwiftyAttributes import JXSegmentedView import WHToast /// 商品行情视图容器控制类 class QuoteViewController: BaseViewController { // MARK: - 属性列表 /// 分段控制器 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.delegate = self segmentedView.dataSource = dataSource segmentedView.indicators = [self.indicator] } } /// 内容装载视图 @IBOutlet weak var contentStackView: UIStackView! /// 海南网警 @IBOutlet weak var police: UIButton! /// 检索按钮 @IBOutlet weak var search: UIButton! /// 类目按钮 @IBOutlet weak var category: UIButton! /// segmentedDataSource let dataSource: JXSegmentedTitleDataSource = { $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333) $0.titleNormalFont = .font_16 $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3) $0.titleSelectedFont = .font_18 $0.isTitleColorGradientEnabled = true return $0 } (JXSegmentedTitleDataSource()) /// 热卖商品视图 lazy var hotQuoteController: HotQuoteViewController = { $0.view.frame = contentStackView.bounds return $0 } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "HotQuote") as! HotQuoteViewController) /// 新品上市视图 lazy var newQuoteController: NewQuoteViewController = { $0.view.frame = contentStackView.bounds return $0 } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "NewQuote") as! NewQuoteViewController) /// 特卖商城 lazy var specialSaleController: SpecialSaleViewController = { $0.view.frame = contentStackView.bounds return $0 } (UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "SpecialSale") as! SpecialSaleViewController) // MARK: - 生命周期相关 required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) /// 设置TabBar图片 self.tabBarItem.image = UIImage(named: "quote_normal")?.withRenderingMode(.alwaysOriginal) self.tabBarItem.selectedImage = UIImage(named: "quote_selected")?.withRenderingMode(.alwaysOriginal) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 不需要返回按钮 addBackBarButtonItem(true) /// UI界面显示 buildView() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 刷新数据 dataSource.titles = ["热卖商品", "新品抢购", "竞价商品"] segmentedView.reloadData() /// 隐藏导航栏 self.navigationController?.setNavigationBarHidden(true, animated: true) } // MARK: - 初始化 /// UI界面显示 fileprivate func buildView() { showController(selectedIndex: 0) } // MARK: - 交互相关 @IBAction func onButtonPressed(_ sender: UIButton) { switch sender { case police: /// 海南网警 push(storyboardName: "Feedback", storyboardId: "Feedback") case category: /// 类目 let category = viewController(name: "Category", identifier: "CategoryID") self.navigationController?.pushViewController(category, animated: true) default: break } } // MARK: - UI显示 /// Tab也切换时显示的容器 /// - Parameter index: index fileprivate func showController(selectedIndex index: Int) { switch index { case 0: /// 热卖商品 /// 移出新品上市视图 removeChildren(hotQuoteController) /// 移出新品上市视图 removeChildren(newQuoteController) /// 添加特卖商城视图 addChildren(specialSaleController) case 1: /// 移出新品上市视图 removeChildren(hotQuoteController) /// 移出新品上市视图 removeChildren(specialSaleController) /// 添加特卖商城视图 addChildren(newQuoteController) default: /// 竞价商品 /// 移出新品上市视图 removeChildren(newQuoteController) /// 移出新品上市视图 removeChildren(specialSaleController) /// 添加特卖商城视图 addChildren(hotQuoteController) } } /// addChildren /// - Parameter child: child fileprivate func addChildren(_ child: UIViewController) { if !contentStackView.arrangedSubviews.contains(child.view) { self.addChild(child) child.didMove(toParent: self) contentStackView.addArrangedSubview(child.view) } } /// removeChildren /// - Parameter child: child fileprivate func removeChildren(_ child: UIViewController) { if contentStackView.arrangedSubviews.contains(child.view) { child.removeFromParent() contentStackView.removeArrangedSubview(child.view) child.view.removeFromSuperview() } } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } } extension QuoteViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { /// 显示相应的视图 self.showController(selectedIndex: index) /// 隐藏分类按钮 self.category.isHidden = !(index == 0) } }