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.

258 lines
11 KiB

  1. //
  2. // QuotationViewController.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/11/26.
  6. //
  7. #import "QuotationViewController.h"
  8. #import "CountryModel.h"
  9. #import "MLXYViewController.h" // 马来西亚
  10. #import "CNViewController.h" // 中国
  11. #import "CommonTabBar.h"
  12. #import "HomeViewController.h"
  13. #import "MyViewController.h"
  14. @interface QuotationViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
  15. @property (nonatomic, strong) CommonTabBar *commonTabBar;
  16. @property (nonatomic, strong) NSArray<CountryModel *> *countries; // 国家列表
  17. @property (nonatomic, strong) UIViewController *currentChildVC; // 当前显示的子控制器
  18. @property (nonatomic, strong) UICollectionView *countryTabBar; // 国家选择标签栏
  19. @property (nonatomic, strong) UIView *contentContainer; // 子控制器容器
  20. @property (nonatomic, assign) NSInteger selectedIndex; // 当前选中的国家索引
  21. // 顶部
  22. @property (nonatomic, strong) UIImageView *logoImage; // logo
  23. @property (nonatomic, strong) UITextField *searchField; // 搜索框
  24. @property (nonatomic, strong) UIImageView *flagIcon; // 国旗
  25. @end
  26. @implementation QuotationViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.view.backgroundColor = [UIColor blackColor];
  30. [self initCountries];
  31. [self setupSubviews];
  32. [self setupConstraints];
  33. // 默认选中第一个
  34. self.selectedIndex = 0;
  35. [self switchToCountryAtIndex:self.selectedIndex];
  36. }
  37. #pragma mark - 初始化国家数据
  38. - (void)initCountries {
  39. self.countries = @[
  40. [CountryModel modelWithCode:@"MY"
  41. name:@"马来西亚"
  42. vcClassName:NSStringFromClass([MLXYViewController class])],
  43. [CountryModel modelWithCode:@"CN"
  44. name:@"中国"
  45. vcClassName:NSStringFromClass([CNViewController class])]
  46. ];
  47. }
  48. #pragma mark - 创建子视图
  49. - (void)setupSubviews {
  50. self.logoImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
  51. self.logoImage.contentMode = UIViewContentModeScaleAspectFit;
  52. self.logoImage.translatesAutoresizingMaskIntoConstraints = NO;
  53. [self.view addSubview:self.logoImage];
  54. self.searchField = [[UITextField alloc] init];
  55. self.searchField.placeholder = @"股票名称/代码";
  56. NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"股票名称/代码" attributes:@{
  57. NSForegroundColorAttributeName: [UIColor whiteColor]
  58. }];
  59. self.searchField.attributedPlaceholder = placeholder;
  60. self.searchField.textColor = [UIColor whiteColor];
  61. self.searchField.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
  62. self.searchField.layer.cornerRadius = 15;
  63. self.searchField.leftView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"magnifyingglass"]];
  64. self.searchField.leftViewMode = UITextFieldViewModeAlways;
  65. self.searchField.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.view addSubview:self.searchField];
  67. self.flagIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
  68. self.flagIcon.contentMode = UIViewContentModeScaleAspectFit;
  69. self.flagIcon.translatesAutoresizingMaskIntoConstraints = NO;
  70. [self.view addSubview:self.flagIcon];
  71. UICollectionViewFlowLayout *tabLayout = [[UICollectionViewFlowLayout alloc] init];
  72. tabLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 横向滚动
  73. tabLayout.itemSize = CGSizeMake(100, 40); // 每个标签宽100,高40
  74. tabLayout.minimumInteritemSpacing = 0;
  75. self.countryTabBar = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:tabLayout];
  76. self.countryTabBar.backgroundColor = [UIColor blackColor];
  77. self.countryTabBar.delegate = self;
  78. self.countryTabBar.dataSource = self;
  79. [self.countryTabBar registerClass:[UICollectionViewCell class]// 单元格复用类
  80. forCellWithReuseIdentifier:@"CountryTabCell"];
  81. self.countryTabBar.translatesAutoresizingMaskIntoConstraints = NO;
  82. [self.view addSubview:self.countryTabBar];
  83. // 5. 内容容器,这个承接子视图
  84. self.contentContainer = [[UIView alloc] init];
  85. self.contentContainer.backgroundColor = [UIColor blackColor];
  86. self.contentContainer.translatesAutoresizingMaskIntoConstraints = NO;
  87. [self.view addSubview:self.contentContainer];
  88. NSArray *tabItems = @[
  89. @{@"title":@"首页", @"icon":@"house.fill", @"selected":@NO},
  90. @{@"title":@"行情", @"icon":@"chart.line.uptrend.xyaxis", @"selected":@YES},
  91. @{@"title":@"自选", @"icon":@"plus", @"selected":@NO},
  92. @{@"title":@"我的", @"icon":@"person", @"selected":@NO}
  93. ];
  94. self.commonTabBar = [[CommonTabBar alloc] initWithTabItems:tabItems];
  95. self.commonTabBar.delegate = self;
  96. self.commonTabBar.translatesAutoresizingMaskIntoConstraints = NO;
  97. [self.view addSubview:self.commonTabBar];
  98. }
  99. #pragma mark - 布局约束
  100. - (void)setupConstraints {
  101. // Logo
  102. [NSLayoutConstraint activateConstraints:@[
  103. [self.logoImage.centerYAnchor constraintEqualToAnchor:self.searchField.centerYAnchor],
  104. [self.logoImage.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15],
  105. [self.logoImage.widthAnchor constraintEqualToConstant:40],
  106. [self.logoImage.heightAnchor constraintEqualToConstant:40]
  107. ]];
  108. // 搜索框
  109. [NSLayoutConstraint activateConstraints:@[
  110. [self.searchField.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:50],
  111. [self.searchField.leadingAnchor constraintEqualToAnchor:self.logoImage.trailingAnchor constant:10],
  112. [self.searchField.trailingAnchor constraintEqualToAnchor:self.flagIcon.leadingAnchor constant:-10],
  113. [self.searchField.heightAnchor constraintEqualToConstant:30]
  114. ]];
  115. // 国旗
  116. [NSLayoutConstraint activateConstraints:@[
  117. [self.flagIcon.centerYAnchor constraintEqualToAnchor:self.searchField.centerYAnchor],
  118. [self.flagIcon.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-15],
  119. [self.flagIcon.widthAnchor constraintEqualToConstant:40],
  120. [self.flagIcon.heightAnchor constraintEqualToConstant:40]
  121. ]];
  122. // 国家
  123. [NSLayoutConstraint activateConstraints:@[
  124. [self.countryTabBar.topAnchor constraintEqualToAnchor:self.searchField.bottomAnchor constant:15],
  125. [self.countryTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  126. [self.countryTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  127. [self.countryTabBar.heightAnchor constraintEqualToConstant:40]
  128. ]];
  129. // 子视图容器
  130. [NSLayoutConstraint activateConstraints:@[
  131. [self.contentContainer.topAnchor constraintEqualToAnchor:self.countryTabBar.bottomAnchor],
  132. [self.contentContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  133. [self.contentContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  134. [self.contentContainer.bottomAnchor constraintEqualToAnchor:self.commonTabBar.topAnchor]
  135. ]];
  136. // 底部TabBar
  137. [NSLayoutConstraint activateConstraints:@[
  138. [self.commonTabBar.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
  139. [self.commonTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  140. [self.commonTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
  141. ]];
  142. }
  143. #pragma mark - 切换子控制器
  144. - (void)switchToCountryAtIndex:(NSInteger)index {
  145. // 先移除
  146. if (self.currentChildVC) {
  147. [self.currentChildVC willMoveToParentViewController:nil];
  148. [self.currentChildVC.view removeFromSuperview];
  149. [self.currentChildVC removeFromParentViewController];
  150. self.currentChildVC = nil;
  151. }
  152. // 创建新的
  153. CountryModel *selectedCountry = self.countries[index];
  154. Class childVCClass = NSClassFromString(selectedCountry.vcClassName);// 字符串转Class????
  155. UIViewController *childVC = [[childVCClass alloc] init];
  156. // 添加
  157. [self addChildViewController:childVC];
  158. childVC.view.frame = self.contentContainer.bounds;// 适配容器
  159. childVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;// 自适应容器大小
  160. [self.contentContainer addSubview:childVC.view];
  161. [childVC didMoveToParentViewController:self];
  162. self.currentChildVC = childVC;
  163. }
  164. #pragma mark - 国家数据源
  165. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  166. return self.countries.count;
  167. }
  168. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  169. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CountryTabCell" forIndexPath:indexPath];
  170. cell.backgroundColor = [UIColor blackColor];
  171. // 国家名称
  172. UILabel *nameLabel = [cell viewWithTag:100];
  173. if (!nameLabel) {
  174. nameLabel = [[UILabel alloc] init];
  175. nameLabel.tag = 100;
  176. nameLabel.textColor = [UIColor whiteColor];
  177. nameLabel.font = [UIFont systemFontOfSize:15];
  178. nameLabel.textAlignment = NSTextAlignmentCenter;
  179. [cell.contentView addSubview:nameLabel];
  180. nameLabel.frame = cell.contentView.bounds;// 撑满单元格
  181. }
  182. nameLabel.text = self.countries[indexPath.item].name;
  183. // 选中下划线
  184. UIView *selectedLine = [cell viewWithTag:101];
  185. if (!selectedLine) {
  186. selectedLine = [[UIView alloc] init];
  187. selectedLine.tag = 101;
  188. selectedLine.backgroundColor = [UIColor blueColor];
  189. [cell.contentView addSubview:selectedLine];
  190. selectedLine.frame = CGRectMake(30, cell.contentView.bounds.size.height - 7, 40, 4);
  191. }
  192. selectedLine.hidden = !(indexPath.item == self.selectedIndex);
  193. return cell;
  194. }
  195. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  196. self.selectedIndex = indexPath.item;
  197. [self.countryTabBar reloadData]; // 刷新标签选中状态
  198. [self switchToCountryAtIndex:indexPath.item]; // 切换到对应国家页面
  199. }
  200. #pragma mark - 底部跳转方法
  201. - (void)tabBarDidSelectIndex:(NSInteger)index {
  202. switch (index) {
  203. case 0:
  204. [self pushHome];
  205. break;
  206. case 1:
  207. break;
  208. case 2:
  209. break;
  210. case 3:
  211. [self pushMine];
  212. break;
  213. }
  214. }
  215. - (void) pushMine {
  216. MyViewController *myViewController = [[MyViewController alloc] init];
  217. [self.navigationController pushViewController:myViewController animated:YES];
  218. }
  219. - (void) pushHome {
  220. HomeViewController *myViewController = [[HomeViewController alloc] init];
  221. [self.navigationController pushViewController:myViewController animated:YES];
  222. }
  223. @end