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.

267 lines
11 KiB

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