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.

634 lines
29 KiB

  1. //
  2. // HomeViewController.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/11/18.
  6. //
  7. #import <UIKit/UIKit.h>
  8. #import "HomeViewController.h"
  9. #import "MyViewController.h"
  10. #import "CommonTabBar.h"
  11. #import "QuotationViewController.h"
  12. @implementation HomeViewController
  13. - (void)viewDidLoad {
  14. self.view.backgroundColor = [UIColor blackColor];
  15. [self setupData];
  16. [self setupSubviews];
  17. [self setupConstraints];
  18. [self selectButton:self.courseBtn];// 课程
  19. }
  20. -(void) setupSubviews{
  21. // 搜索框左边logo
  22. _logoImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
  23. _logoImage.contentMode = UIViewContentModeScaleAspectFit;// 保持比例
  24. _logoImage.translatesAutoresizingMaskIntoConstraints = NO;
  25. [self.view addSubview:_logoImage];
  26. // 搜索框
  27. _searchField = [[UITextField alloc] init];
  28. _searchField.placeholder = @"股票名称/代码";
  29. NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"股票名称/代码" attributes:@{
  30. NSForegroundColorAttributeName: [UIColor whiteColor]
  31. }];
  32. _searchField.attributedPlaceholder = placeholder;
  33. _searchField.textColor = [UIColor whiteColor];
  34. _searchField.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
  35. _searchField.layer.cornerRadius = 15;
  36. _searchField.leftView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"magnifyingglass"]];
  37. _searchField.leftViewMode = UITextFieldViewModeAlways;
  38. _searchField.translatesAutoresizingMaskIntoConstraints = NO;
  39. [self.view addSubview:_searchField];
  40. // 市场配置
  41. _flagIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"singapore_flag"]];
  42. _flagIcon.contentMode = UIViewContentModeScaleAspectFit;
  43. _flagIcon.translatesAutoresizingMaskIntoConstraints = NO;
  44. [self.view addSubview:_flagIcon];
  45. // 消息
  46. _messageBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  47. UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
  48. config.image = [UIImage systemImageNamed:@"message"];
  49. config.baseForegroundColor = [UIColor whiteColor];
  50. // 富文本
  51. NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"15" attributes:@{
  52. NSFontAttributeName: [UIFont systemFontOfSize:10],
  53. NSForegroundColorAttributeName: [UIColor whiteColor]
  54. }];
  55. config.attributedTitle = attributedTitle;
  56. config.imagePadding = -5;
  57. config.contentInsets = NSDirectionalEdgeInsetsMake(0, 0, 0, 0);// 内容边距
  58. [_messageBtn setConfiguration:config];
  59. _messageBtn.translatesAutoresizingMaskIntoConstraints = NO;
  60. [self.view addSubview:_messageBtn];
  61. // 功能按钮数组初始化
  62. _functionBtns = [NSMutableArray array];
  63. for (NSDictionary *dict in _functionIcons) {
  64. UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
  65. UIImage *logoImage = [UIImage imageNamed:@"logo"];
  66. UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 40), NO, 0);// 开启图形上下文,((尺寸,尺寸),是否不透明,缩放因子)
  67. [logoImage drawInRect:CGRectMake(0, 0, 40, 40)];// 调整,等比缩放
  68. UIImage *resizedLogo = UIGraphicsGetImageFromCurrentImageContext();// 获得调整后的
  69. UIGraphicsEndImageContext();// 关闭图形上下文
  70. config.image = resizedLogo;
  71. // 按钮文字,富文本
  72. NSAttributedString *titleAttr = [[NSAttributedString alloc] initWithString:dict[@"title"] attributes:@{
  73. NSFontAttributeName: [UIFont systemFontOfSize:13],
  74. NSForegroundColorAttributeName: [UIColor whiteColor]
  75. }];
  76. config.attributedTitle = titleAttr;
  77. config.imagePlacement = NSDirectionalRectEdgeTop;
  78. config.imagePadding = 5;
  79. config.baseForegroundColor = [UIColor whiteColor];
  80. UIButton *btn = [UIButton buttonWithConfiguration:config primaryAction:nil];
  81. btn.titleLabel.font = [UIFont systemFontOfSize:12];
  82. btn.translatesAutoresizingMaskIntoConstraints = NO;
  83. [self.view addSubview:btn];
  84. [_functionBtns addObject:btn];
  85. }
  86. // 轮播图视图
  87. _bannerScrollView = [[UIScrollView alloc] init];
  88. _bannerScrollView.showsHorizontalScrollIndicator = NO;// 隐藏水平滚动条
  89. _bannerScrollView.pagingEnabled = YES;// 分页
  90. _bannerScrollView.delegate = self;
  91. _bannerScrollView.translatesAutoresizingMaskIntoConstraints = NO;
  92. [self.view addSubview:_bannerScrollView];
  93. // 轮播图内容容器
  94. UIView *bannerContentView = [[UIView alloc] init];
  95. bannerContentView.translatesAutoresizingMaskIntoConstraints = NO;
  96. [_bannerScrollView addSubview:bannerContentView];
  97. // 轮播图片
  98. NSArray *bannerImageNames = @[@"banner2", @"banner1", @"banner2", @"banner1"];
  99. NSMutableArray *bannerImages = [NSMutableArray array];
  100. for (int i = 0; i < bannerImageNames.count; i++) {
  101. UIImageView *bannerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bannerImageNames[i]]];
  102. bannerImage.contentMode = UIViewContentModeScaleAspectFill;// 填充
  103. bannerImage.clipsToBounds = YES;// 裁剪超出的
  104. bannerImage.translatesAutoresizingMaskIntoConstraints = NO;
  105. [bannerContentView addSubview:bannerImage];
  106. [bannerImages addObject:bannerImage];
  107. [bannerImage.heightAnchor constraintEqualToAnchor:bannerContentView.heightAnchor].active = YES;
  108. [bannerImage.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;
  109. if (i == 0) {
  110. [bannerImage.leadingAnchor constraintEqualToAnchor:bannerContentView.leadingAnchor].active = YES;
  111. } else {
  112. UIImageView *prevImage = bannerImages[i-1];
  113. [bannerImage.leadingAnchor constraintEqualToAnchor:prevImage.trailingAnchor].active = YES;
  114. }
  115. }
  116. // 内容视图宽度(总图片数 * 屏幕宽度)
  117. [bannerContentView.widthAnchor constraintEqualToConstant:self.view.bounds.size.width * bannerImages.count].active = YES;
  118. [NSLayoutConstraint activateConstraints:@[
  119. [bannerContentView.leadingAnchor constraintEqualToAnchor:_bannerScrollView.leadingAnchor],
  120. [bannerContentView.trailingAnchor constraintEqualToAnchor:_bannerScrollView.trailingAnchor],
  121. [bannerContentView.topAnchor constraintEqualToAnchor:_bannerScrollView.topAnchor],
  122. [bannerContentView.bottomAnchor constraintEqualToAnchor:_bannerScrollView.bottomAnchor],
  123. [bannerContentView.heightAnchor constraintEqualToAnchor:_bannerScrollView.heightAnchor],
  124. ]];
  125. // 初始第一张
  126. [_bannerScrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:NO];
  127. // 页码
  128. _bannerPageControl = [[UIPageControl alloc] init];
  129. _bannerPageControl.currentPageIndicatorTintColor = [UIColor blueColor];
  130. _bannerPageControl.pageIndicatorTintColor = [UIColor lightGrayColor];// 其他页码
  131. _bannerPageControl.numberOfPages = 2;
  132. _bannerPageControl.translatesAutoresizingMaskIntoConstraints = NO;
  133. [self.view addSubview:_bannerPageControl];
  134. // 初始化定时器(2秒一切)
  135. _bannerTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoScrollBanner) userInfo:nil repeats:YES];
  136. [[NSRunLoop mainRunLoop] addTimer:_bannerTimer forMode:NSRunLoopCommonModes];
  137. _currentBannerIndex = 0;
  138. // 课程咨询活动的容器
  139. self.tabContainer = [[UIView alloc] init];
  140. self.tabContainer.backgroundColor = [UIColor blackColor];
  141. self.tabContainer.translatesAutoresizingMaskIntoConstraints = NO;
  142. [self.view addSubview:self.tabContainer];
  143. // 课程
  144. _courseBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  145. [_courseBtn setTitle:@"课程" forState:UIControlStateNormal];
  146. _courseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  147. [_courseBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  148. [_courseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  149. _courseBtn.backgroundColor = [UIColor blackColor];
  150. //[_courseBtn setBackgroundColor:[UIColor blackColor]];
  151. [_courseBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  152. _courseBtn.tag = 100;
  153. _courseBtn.translatesAutoresizingMaskIntoConstraints = NO;
  154. [self.tabContainer addSubview:_courseBtn];
  155. // 资讯
  156. _infoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  157. [_infoBtn setTitle:@"资讯" forState:UIControlStateNormal];
  158. _infoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  159. //_infoBtn.backgroundColor = [UIColor blackColor];
  160. [_infoBtn setBackgroundColor:[UIColor blackColor]];
  161. [_infoBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  162. [_infoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  163. [_infoBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  164. _infoBtn.tag = 101;
  165. _infoBtn.translatesAutoresizingMaskIntoConstraints = NO;
  166. [self.tabContainer addSubview:_infoBtn];
  167. // 活动
  168. _activityBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  169. [_activityBtn setTitle:@"活动" forState:UIControlStateNormal];
  170. _activityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  171. //_activityBtn.backgroundColor = [UIColor blackColor];
  172. [_activityBtn setBackgroundColor:[UIColor blackColor]];
  173. [_activityBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  174. [_activityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  175. [_activityBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  176. _activityBtn.tag = 102;
  177. _activityBtn.translatesAutoresizingMaskIntoConstraints = NO;
  178. [self.tabContainer addSubview:_activityBtn];
  179. // 课程下划线
  180. _courseUnderline = [[UIView alloc] init];
  181. _courseUnderline.backgroundColor = [UIColor blueColor];
  182. _courseUnderline.hidden = YES;
  183. _courseUnderline.translatesAutoresizingMaskIntoConstraints = NO;
  184. [self.tabContainer addSubview:_courseUnderline];
  185. // 资讯下划线
  186. _infoUnderline = [[UIView alloc] init];
  187. _infoUnderline.backgroundColor = [UIColor blueColor];
  188. _infoUnderline.hidden = YES;
  189. _infoUnderline.translatesAutoresizingMaskIntoConstraints = NO;
  190. [self.tabContainer addSubview:_infoUnderline];
  191. // 活动下划线
  192. _activityUnderline = [[UIView alloc] init];
  193. _activityUnderline.backgroundColor = [UIColor blueColor];
  194. _activityUnderline.hidden = YES;
  195. _activityUnderline.translatesAutoresizingMaskIntoConstraints = NO;
  196. [self.tabContainer addSubview:_activityUnderline];
  197. // 课程列表TableView
  198. _courseTableView = [[UITableView alloc] init];
  199. _courseTableView.backgroundColor = [UIColor blackColor];
  200. _courseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 隐藏分割线
  201. [_courseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CourseCell"];
  202. _courseTableView.delegate = self;
  203. _courseTableView.dataSource = self;// 数据源
  204. _courseTableView.translatesAutoresizingMaskIntoConstraints = NO;
  205. [self.view addSubview:_courseTableView];
  206. // 红包
  207. _redPacketIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
  208. _redPacketIcon.contentMode = UIViewContentModeScaleAspectFit;
  209. _redPacketIcon.translatesAutoresizingMaskIntoConstraints = NO;
  210. [self.view addSubview:_redPacketIcon];
  211. NSArray *tabItems = @[
  212. @{@"title":@"首页", @"icon":@"house.fill", @"selected":@YES},
  213. @{@"title":@"行情", @"icon":@"chart.line.uptrend.xyaxis", @"selected":@NO},
  214. @{@"title":@"自选", @"icon":@"plus", @"selected":@NO},
  215. @{@"title":@"我的", @"icon":@"person", @"selected":@NO}
  216. ];
  217. self.commonTabBar = [[CommonTabBar alloc] initWithTabItems:tabItems];
  218. self.commonTabBar.delegate = self;
  219. [self.view addSubview:self.commonTabBar];
  220. // 登录提示
  221. _successLabel = [[UILabel alloc] init];
  222. _successLabel.text = @"登录成功";
  223. _successLabel.font = [UIFont systemFontOfSize:20];
  224. _successLabel.textColor = [UIColor whiteColor];
  225. _successLabel.numberOfLines = 0;
  226. _successLabel.translatesAutoresizingMaskIntoConstraints = NO;
  227. [self.view addSubview:_successLabel];
  228. // 标签容器和按钮约束
  229. [NSLayoutConstraint activateConstraints:@[
  230. // 标签容器,轮播图下15,左15,右15,高30
  231. [self.tabContainer.topAnchor constraintEqualToAnchor: _bannerScrollView.bottomAnchor constant:15],
  232. [self.tabContainer.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15],
  233. [self.tabContainer.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-15],
  234. [self.tabContainer.heightAnchor constraintEqualToConstant:30],
  235. // 按钮均分宽度
  236. // 课程左齐容器,垂直居中,宽度1/3
  237. [_courseBtn.leadingAnchor constraintEqualToAnchor: self.tabContainer.leadingAnchor],
  238. [_courseBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor],
  239. [_courseBtn.widthAnchor constraintEqualToAnchor: self.tabContainer.widthAnchor multiplier:1.0/3.0],
  240. // 咨询左对齐课程右侧
  241. [_infoBtn.leadingAnchor constraintEqualToAnchor: _courseBtn.trailingAnchor],
  242. [_infoBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor],
  243. [_infoBtn.widthAnchor constraintEqualToAnchor: self.tabContainer.widthAnchor multiplier:1.0/3.0],
  244. // 活动左对齐咨询右侧
  245. [_activityBtn.leadingAnchor constraintEqualToAnchor: _infoBtn.trailingAnchor],
  246. [_activityBtn.trailingAnchor constraintEqualToAnchor: self.tabContainer.trailingAnchor],
  247. [_activityBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor],
  248. // 下划线约束
  249. // 底部对齐容器,在button下方正中,宽302
  250. [_courseUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor],
  251. [_courseUnderline.centerXAnchor constraintEqualToAnchor: _courseBtn.centerXAnchor],
  252. [_courseUnderline.widthAnchor constraintEqualToConstant:30],
  253. [_courseUnderline.heightAnchor constraintEqualToConstant:2],
  254. [_infoUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor],
  255. [_infoUnderline.centerXAnchor constraintEqualToAnchor: _infoBtn.centerXAnchor],
  256. [_infoUnderline.widthAnchor constraintEqualToConstant:30],
  257. [_infoUnderline.heightAnchor constraintEqualToConstant:2],
  258. [_activityUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor],
  259. [_activityUnderline.centerXAnchor constraintEqualToAnchor: _activityBtn.centerXAnchor],
  260. [_activityUnderline.widthAnchor constraintEqualToConstant:30],
  261. [_activityUnderline.heightAnchor constraintEqualToConstant:2],
  262. ]];
  263. }
  264. -(void) setupConstraints{
  265. // 登录成功提示
  266. [NSLayoutConstraint activateConstraints:@[
  267. [_successLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],// 水平居中
  268. [_successLabel.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],// 垂直居中
  269. [_successLabel.widthAnchor constraintEqualToConstant:80],
  270. [_successLabel.heightAnchor constraintEqualToConstant:30],
  271. ]];
  272. // Logo图标 搜索框高度下垂直居中,左边距15,宽4040
  273. [NSLayoutConstraint activateConstraints:@[
  274. [_logoImage.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor],
  275. [_logoImage.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15],
  276. [_logoImage.widthAnchor constraintEqualToConstant:40],
  277. [_logoImage.heightAnchor constraintEqualToConstant:40],
  278. ]];
  279. // 搜索栏 父视图下50logo左10,国旗左10,高30
  280. [NSLayoutConstraint activateConstraints:@[
  281. [_searchField.topAnchor constraintEqualToAnchor: self.view.topAnchor constant:50],
  282. [_searchField.leadingAnchor constraintEqualToAnchor: _logoImage.trailingAnchor constant:10],
  283. [_searchField.trailingAnchor constraintEqualToAnchor: _flagIcon.leadingAnchor constant:-10],
  284. [_searchField.heightAnchor constraintEqualToConstant:30],
  285. ]];
  286. // 国旗图标
  287. [NSLayoutConstraint activateConstraints:@[
  288. [_flagIcon.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor],
  289. [_flagIcon.trailingAnchor constraintEqualToAnchor: _messageBtn.leadingAnchor constant:-10],
  290. [_flagIcon.widthAnchor constraintEqualToConstant:40],
  291. [_flagIcon.heightAnchor constraintEqualToConstant:40],
  292. ]];
  293. // 消息按钮
  294. [NSLayoutConstraint activateConstraints:@[
  295. [_messageBtn.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor],
  296. [_messageBtn.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-15],
  297. [_messageBtn.widthAnchor constraintEqualToConstant:40],
  298. [_messageBtn.heightAnchor constraintEqualToConstant:40],
  299. ]];
  300. // 功能按钮(两行四列,均匀占满整行)
  301. for (int i = 0; i < _functionBtns.count; i++) {
  302. UIButton *btn = _functionBtns[i];
  303. NSLayoutConstraint *topConstraint;
  304. NSLayoutConstraint *widthConstraint = [btn.widthAnchor constraintEqualToAnchor: self.view.widthAnchor multiplier:0.25 constant:-15];
  305. NSLayoutConstraint *heightConstraint = [btn.heightAnchor constraintEqualToConstant:70];
  306. if (i < 4) {
  307. topConstraint = [btn.topAnchor constraintEqualToAnchor: _searchField.bottomAnchor constant:20];
  308. NSLayoutConstraint *leadingConstraint;
  309. if (i == 0) {
  310. leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15];
  311. } else {
  312. leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: _functionBtns[i-1].trailingAnchor constant:0];
  313. }
  314. [NSLayoutConstraint activateConstraints:@[topConstraint, leadingConstraint, widthConstraint, heightConstraint]];
  315. } else {
  316. topConstraint = [btn.topAnchor constraintEqualToAnchor: _functionBtns[i-4].bottomAnchor constant:10];
  317. NSLayoutConstraint *leadingConstraint;
  318. if (i == 4) {
  319. leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15];
  320. } else {
  321. leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: _functionBtns[i-1].trailingAnchor constant:0];
  322. }
  323. [NSLayoutConstraint activateConstraints:@[topConstraint, leadingConstraint, widthConstraint, heightConstraint]];
  324. }
  325. }
  326. // 轮播图ScrollView 顶部距离最后一个功能按钮20,左右对齐父视图,高150
  327. [NSLayoutConstraint activateConstraints:@[
  328. [_bannerScrollView.topAnchor constraintEqualToAnchor: _functionBtns.lastObject.bottomAnchor constant:20],
  329. [_bannerScrollView.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor],
  330. [_bannerScrollView.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor],
  331. [_bannerScrollView.heightAnchor constraintEqualToConstant:150],
  332. ]];
  333. // 轮播图PageControl,底部距离轮播图5
  334. [NSLayoutConstraint activateConstraints:@[
  335. [_bannerPageControl.bottomAnchor constraintEqualToAnchor: _bannerScrollView.bottomAnchor constant:-5],
  336. [_bannerPageControl.centerXAnchor constraintEqualToAnchor: self.view.centerXAnchor],
  337. ]];
  338. // 课程列表 顶部距离标签容器20,左右对齐父视图,底部tabBar
  339. [NSLayoutConstraint activateConstraints:@[
  340. [_courseTableView.topAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor constant:20],
  341. [_courseTableView.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor],
  342. [_courseTableView.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor],
  343. [_courseTableView.bottomAnchor constraintEqualToAnchor: _commonTabBar.topAnchor]
  344. ]];
  345. // 红包图标
  346. [NSLayoutConstraint activateConstraints:@[
  347. [_redPacketIcon.bottomAnchor constraintEqualToAnchor: _courseTableView.bottomAnchor constant:-200],
  348. [_redPacketIcon.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-20],
  349. [_redPacketIcon.widthAnchor constraintEqualToConstant:60],
  350. [_redPacketIcon.heightAnchor constraintEqualToConstant:60],
  351. ]];
  352. // 底部页面按钮 底部对齐安全区域,左右对齐父视图
  353. [_commonTabBar.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor].active = YES;
  354. [_commonTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
  355. [_commonTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
  356. }
  357. - (void)setupData {
  358. _functionIcons = @[// 先都用logo
  359. @{@"title":@"全球指数"},
  360. @{@"title":@"浏览历史"},
  361. @{@"title":@"机构"},
  362. @{@"title":@"弘历学堂"},
  363. @{@"title":@"自选"},
  364. @{@"title":@"商店"},
  365. @{@"title":@"猎庄选股"},
  366. @{@"title":@"智能机器人"}
  367. ];
  368. _courseData = @[
  369. @{@"cover":@"course1", @"title":@"John Lu & Robert Xu “发财线”课程回顾!", @"lecturer":@"Homily Lecturer"},
  370. @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"kaslana"},
  371. @{@"cover":@"course3", @"title":@"论持久战", @"lecturer":@"教员先生"}
  372. ];
  373. }
  374. #pragma mark - 标签方法
  375. - (void)tabButtonTapped:(UIButton *)sender {
  376. [self selectButton:sender];
  377. // 根据标签切换
  378. switch (sender.tag) {
  379. case 100:
  380. _courseData = @[
  381. @{@"cover":@"course1", @"title":@"John Lu & Robert Xu “发财线”课程回顾!", @"lecturer":@"Homily Lecturer"},
  382. @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"kaslana"},
  383. @{@"cover":@"course3", @"title":@"论持久战", @"lecturer":@"教员先生"}
  384. ];
  385. break;
  386. case 101:
  387. _courseData = @[
  388. @{@"cover":@"news1", @"title":@"objective-c从入门到入土", @"lecturer":@"财经资讯"},
  389. @{@"cover":@"news2", @"title":@"线程池核心参数详解", @"lecturer":@"财经资讯"},
  390. @{@"cover":@"news3", @"title":@"redis集群-主从、哨兵、分片", @"lecturer":@"财经资讯"}
  391. ];
  392. break;
  393. case 102:
  394. _courseData = @[
  395. @{@"cover":@"event1", @"title":@"SpringCloud入门到放弃", @"lecturer":@"活动预告"},
  396. @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"活动预告"},
  397. @{@"cover":@"event3", @"title":@"mySQL:从删库到跑路", @"lecturer":@"活动预告"}
  398. ];
  399. break;
  400. }
  401. [_courseTableView reloadData];
  402. }
  403. - (void) pushMine {
  404. MyViewController *myViewController = [[MyViewController alloc] init];
  405. [self.navigationController pushViewController:myViewController animated:YES];
  406. }
  407. - (void)selectButton:(UIButton *)selectedButton {
  408. _courseBtn.selected = NO;
  409. _infoBtn.selected = NO;
  410. _activityBtn.selected = NO;
  411. _courseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  412. _infoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  413. _activityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  414. _courseUnderline.hidden = YES;
  415. _infoUnderline.hidden = YES;
  416. _activityUnderline.hidden = YES;
  417. // 设置选中按钮状态
  418. selectedButton.selected = YES;
  419. selectedButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  420. // 显示对应下划线
  421. if (selectedButton == _courseBtn) {
  422. _courseUnderline.hidden = NO;
  423. } else if (selectedButton == _infoBtn) {
  424. _infoUnderline.hidden = NO;
  425. } else if (selectedButton == _activityBtn) {
  426. _activityUnderline.hidden = NO;
  427. }
  428. }
  429. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  430. if (scrollView == _bannerScrollView) {
  431. CGFloat screenWidth = self.view.bounds.size.width;
  432. NSInteger currentOffsetX = scrollView.contentOffset.x;
  433. NSInteger page = currentOffsetX / screenWidth;
  434. // 首尾拼接图无缝跳转
  435. if (page == 0) {
  436. page = 2;
  437. [scrollView setContentOffset:CGPointMake(screenWidth * page, 0) animated:NO];
  438. } else if (page == 3) {
  439. page = 1;
  440. [scrollView setContentOffset:CGPointMake(screenWidth * page, 0) animated:NO];
  441. }
  442. _currentBannerIndex = (page - 1) % 2;
  443. _bannerPageControl.currentPage = _currentBannerIndex;
  444. // 重启定时器
  445. [_bannerTimer invalidate];
  446. _bannerTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
  447. target:self
  448. selector:@selector(autoScrollBanner)
  449. userInfo:nil
  450. repeats:YES];
  451. }
  452. }
  453. - (void)autoScrollBanner {
  454. CGFloat screenWidth = self.view.bounds.size.width;
  455. // 直接基于真实索引计算下一页,避免contentOffset的延迟问题
  456. _currentBannerIndex = (_currentBannerIndex + 1) % 2; // 0→1→0循环
  457. NSInteger targetPage = _currentBannerIndex + 1; // 对应拼接后的页面(1/2
  458. // 处理尾部拼接图的无缝跳转
  459. if (targetPage > 2) {
  460. targetPage = 1;
  461. [_bannerScrollView setContentOffset:CGPointMake(screenWidth * targetPage, 0) animated:NO];
  462. }
  463. [_bannerScrollView setContentOffset:CGPointMake(screenWidth * targetPage, 0) animated:YES];
  464. _bannerPageControl.currentPage = _currentBannerIndex;
  465. }
  466. - (void) pushQuotation {
  467. QuotationViewController *quotationViewController = [[QuotationViewController alloc] init];
  468. [self.navigationController pushViewController:quotationViewController animated:YES];
  469. }
  470. - (void)tabBarDidSelectIndex:(NSInteger)index {
  471. if (index == 3) {
  472. [self pushMine];
  473. }
  474. else if (index == 0) {
  475. }
  476. else if (index == 1) {
  477. [self pushQuotation];
  478. }
  479. else if (index == 2) {
  480. // 自选
  481. }
  482. }
  483. #pragma mark - 视图即将消失
  484. - (void)viewWillDisappear:(BOOL)animated {
  485. [super viewWillDisappear:animated];
  486. [_bannerTimer invalidate];// 销毁定时器
  487. _bannerTimer = nil;
  488. }
  489. - (void)viewDidAppear:(BOOL)animated {
  490. [super viewDidAppear:animated];
  491. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  492. [UIView animateWithDuration:0.3 animations:^{
  493. self->_successLabel.alpha = 0;
  494. } completion:^(BOOL finished) {
  495. [self->_successLabel removeFromSuperview];
  496. }];
  497. });
  498. }
  499. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  500. return _courseData.count;
  501. }
  502. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  503. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseCell" forIndexPath:indexPath];
  504. cell.backgroundColor = [UIColor blackColor];
  505. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  506. for (UIView *subview in cell.contentView.subviews) {
  507. [subview removeFromSuperview];
  508. }
  509. NSDictionary *course = _courseData[indexPath.row];
  510. UIImageView *coverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:course[@"cover"]]];
  511. coverView.contentMode = UIViewContentModeScaleAspectFill;
  512. coverView.clipsToBounds = YES;
  513. coverView.frame = CGRectMake(15, 10, 140, 100);
  514. [cell.contentView addSubview:coverView];
  515. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 10, 220, 60)];
  516. titleLabel.text = course[@"title"];
  517. titleLabel.textColor = [UIColor whiteColor];
  518. titleLabel.font = [UIFont systemFontOfSize:14];
  519. titleLabel.numberOfLines = 3;
  520. [cell.contentView addSubview:titleLabel];
  521. UILabel *lecturerLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 75, 120, 30)];
  522. lecturerLabel.text = course[@"lecturer"];
  523. lecturerLabel.textColor = [UIColor lightGrayColor];
  524. lecturerLabel.font = [UIFont systemFontOfSize:14];
  525. [cell.contentView addSubview:lecturerLabel];
  526. UIView *bottomLine = [[UIView alloc] init];
  527. bottomLine.backgroundColor = [UIColor lightGrayColor];
  528. bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
  529. [cell.contentView addSubview:bottomLine];
  530. [NSLayoutConstraint activateConstraints:@[
  531. [bottomLine.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:20], // 左边距15
  532. [bottomLine.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor constant:-20], // 右边距15
  533. [bottomLine.bottomAnchor constraintEqualToAnchor:cell.contentView.bottomAnchor],
  534. [bottomLine.heightAnchor constraintEqualToConstant:0.2]
  535. ]];
  536. return cell;
  537. }
  538. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  539. return 120;
  540. }
  541. @end