// // HomeViewController.m // HC // // Created by huilinLi on 2025/11/18. // #import #import "HomeViewController.h" #import "MyViewController.h" #import "CommonTabBar.h" #import "QuotationViewController.h" @implementation HomeViewController - (void)viewDidLoad { self.view.backgroundColor = [UIColor blackColor]; [self setupData]; [self setupSubviews]; [self setupConstraints]; [self selectButton:self.courseBtn];// 课程 } -(void) setupSubviews{ // 搜索框左边logo _logoImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; _logoImage.contentMode = UIViewContentModeScaleAspectFit;// 保持比例 _logoImage.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_logoImage]; // 搜索框 _searchField = [[UITextField alloc] init]; _searchField.placeholder = @"股票名称/代码"; NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"股票名称/代码" attributes:@{ NSForegroundColorAttributeName: [UIColor whiteColor] }]; _searchField.attributedPlaceholder = placeholder; _searchField.textColor = [UIColor whiteColor]; _searchField.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]; _searchField.layer.cornerRadius = 15; _searchField.leftView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"magnifyingglass"]]; _searchField.leftViewMode = UITextFieldViewModeAlways; _searchField.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_searchField]; // 市场配置 _flagIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"singapore_flag"]]; _flagIcon.contentMode = UIViewContentModeScaleAspectFit; _flagIcon.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_flagIcon]; // 消息 _messageBtn = [UIButton buttonWithType:UIButtonTypeSystem]; UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration]; config.image = [UIImage systemImageNamed:@"message"]; config.baseForegroundColor = [UIColor whiteColor]; // 富文本 NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"15" attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName: [UIColor whiteColor] }]; config.attributedTitle = attributedTitle; config.imagePadding = -5; config.contentInsets = NSDirectionalEdgeInsetsMake(0, 0, 0, 0);// 内容边距 [_messageBtn setConfiguration:config]; _messageBtn.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_messageBtn]; // 功能按钮数组初始化 _functionBtns = [NSMutableArray array]; for (NSDictionary *dict in _functionIcons) { UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration]; UIImage *logoImage = [UIImage imageNamed:@"logo"]; UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 40), NO, 0);// 开启图形上下文,((尺寸,尺寸),是否不透明,缩放因子) [logoImage drawInRect:CGRectMake(0, 0, 40, 40)];// 调整,等比缩放 UIImage *resizedLogo = UIGraphicsGetImageFromCurrentImageContext();// 获得调整后的 UIGraphicsEndImageContext();// 关闭图形上下文 config.image = resizedLogo; // 按钮文字,富文本 NSAttributedString *titleAttr = [[NSAttributedString alloc] initWithString:dict[@"title"] attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName: [UIColor whiteColor] }]; config.attributedTitle = titleAttr; config.imagePlacement = NSDirectionalRectEdgeTop; config.imagePadding = 5; config.baseForegroundColor = [UIColor whiteColor]; UIButton *btn = [UIButton buttonWithConfiguration:config primaryAction:nil]; btn.titleLabel.font = [UIFont systemFontOfSize:12]; btn.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:btn]; [_functionBtns addObject:btn]; } // 轮播图视图 _bannerScrollView = [[UIScrollView alloc] init]; _bannerScrollView.showsHorizontalScrollIndicator = NO;// 隐藏水平滚动条 _bannerScrollView.pagingEnabled = YES;// 分页 _bannerScrollView.delegate = self; _bannerScrollView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_bannerScrollView]; // 轮播图内容容器 UIView *bannerContentView = [[UIView alloc] init]; bannerContentView.translatesAutoresizingMaskIntoConstraints = NO; [_bannerScrollView addSubview:bannerContentView]; // 轮播图片 NSArray *bannerImageNames = @[@"banner2", @"banner1", @"banner2", @"banner1"]; NSMutableArray *bannerImages = [NSMutableArray array]; for (int i = 0; i < bannerImageNames.count; i++) { UIImageView *bannerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bannerImageNames[i]]]; bannerImage.contentMode = UIViewContentModeScaleAspectFill;// 填充 bannerImage.clipsToBounds = YES;// 裁剪超出的 bannerImage.translatesAutoresizingMaskIntoConstraints = NO; [bannerContentView addSubview:bannerImage]; [bannerImages addObject:bannerImage]; [bannerImage.heightAnchor constraintEqualToAnchor:bannerContentView.heightAnchor].active = YES; [bannerImage.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES; if (i == 0) { [bannerImage.leadingAnchor constraintEqualToAnchor:bannerContentView.leadingAnchor].active = YES; } else { UIImageView *prevImage = bannerImages[i-1]; [bannerImage.leadingAnchor constraintEqualToAnchor:prevImage.trailingAnchor].active = YES; } } // 内容视图宽度(总图片数 * 屏幕宽度) [bannerContentView.widthAnchor constraintEqualToConstant:self.view.bounds.size.width * bannerImages.count].active = YES; [NSLayoutConstraint activateConstraints:@[ [bannerContentView.leadingAnchor constraintEqualToAnchor:_bannerScrollView.leadingAnchor], [bannerContentView.trailingAnchor constraintEqualToAnchor:_bannerScrollView.trailingAnchor], [bannerContentView.topAnchor constraintEqualToAnchor:_bannerScrollView.topAnchor], [bannerContentView.bottomAnchor constraintEqualToAnchor:_bannerScrollView.bottomAnchor], [bannerContentView.heightAnchor constraintEqualToAnchor:_bannerScrollView.heightAnchor], ]]; // 初始第一张 [_bannerScrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:NO]; // 页码 _bannerPageControl = [[UIPageControl alloc] init]; _bannerPageControl.currentPageIndicatorTintColor = [UIColor blueColor]; _bannerPageControl.pageIndicatorTintColor = [UIColor lightGrayColor];// 其他页码 _bannerPageControl.numberOfPages = 2; _bannerPageControl.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_bannerPageControl]; // 初始化定时器(2秒一切) _bannerTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoScrollBanner) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_bannerTimer forMode:NSRunLoopCommonModes]; _currentBannerIndex = 0; // 课程咨询活动的容器 self.tabContainer = [[UIView alloc] init]; self.tabContainer.backgroundColor = [UIColor blackColor]; self.tabContainer.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:self.tabContainer]; // 课程 _courseBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_courseBtn setTitle:@"课程" forState:UIControlStateNormal]; _courseBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [_courseBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; [_courseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; _courseBtn.backgroundColor = [UIColor blackColor]; //[_courseBtn setBackgroundColor:[UIColor blackColor]]; [_courseBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; _courseBtn.tag = 100; _courseBtn.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_courseBtn]; // 资讯 _infoBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_infoBtn setTitle:@"资讯" forState:UIControlStateNormal]; _infoBtn.titleLabel.font = [UIFont systemFontOfSize:14]; //_infoBtn.backgroundColor = [UIColor blackColor]; [_infoBtn setBackgroundColor:[UIColor blackColor]]; [_infoBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; [_infoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; [_infoBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; _infoBtn.tag = 101; _infoBtn.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_infoBtn]; // 活动 _activityBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_activityBtn setTitle:@"活动" forState:UIControlStateNormal]; _activityBtn.titleLabel.font = [UIFont systemFontOfSize:14]; //_activityBtn.backgroundColor = [UIColor blackColor]; [_activityBtn setBackgroundColor:[UIColor blackColor]]; [_activityBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; [_activityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; [_activityBtn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; _activityBtn.tag = 102; _activityBtn.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_activityBtn]; // 课程下划线 _courseUnderline = [[UIView alloc] init]; _courseUnderline.backgroundColor = [UIColor blueColor]; _courseUnderline.hidden = YES; _courseUnderline.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_courseUnderline]; // 资讯下划线 _infoUnderline = [[UIView alloc] init]; _infoUnderline.backgroundColor = [UIColor blueColor]; _infoUnderline.hidden = YES; _infoUnderline.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_infoUnderline]; // 活动下划线 _activityUnderline = [[UIView alloc] init]; _activityUnderline.backgroundColor = [UIColor blueColor]; _activityUnderline.hidden = YES; _activityUnderline.translatesAutoresizingMaskIntoConstraints = NO; [self.tabContainer addSubview:_activityUnderline]; // 课程列表TableView _courseTableView = [[UITableView alloc] init]; _courseTableView.backgroundColor = [UIColor blackColor]; _courseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 隐藏分割线 [_courseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CourseCell"]; _courseTableView.delegate = self; _courseTableView.dataSource = self;// 数据源 _courseTableView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_courseTableView]; // 红包 _redPacketIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; _redPacketIcon.contentMode = UIViewContentModeScaleAspectFit; _redPacketIcon.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_redPacketIcon]; NSArray *tabItems = @[ @{@"title":@"首页", @"icon":@"house.fill", @"selected":@YES}, @{@"title":@"行情", @"icon":@"chart.line.uptrend.xyaxis", @"selected":@NO}, @{@"title":@"自选", @"icon":@"plus", @"selected":@NO}, @{@"title":@"我的", @"icon":@"person", @"selected":@NO} ]; self.commonTabBar = [[CommonTabBar alloc] initWithTabItems:tabItems]; self.commonTabBar.delegate = self; [self.view addSubview:self.commonTabBar]; // 登录提示 _successLabel = [[UILabel alloc] init]; _successLabel.text = @"登录成功"; _successLabel.font = [UIFont systemFontOfSize:20]; _successLabel.textColor = [UIColor whiteColor]; _successLabel.numberOfLines = 0; _successLabel.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:_successLabel]; // 标签容器和按钮约束 [NSLayoutConstraint activateConstraints:@[ // 标签容器,轮播图下15,左15,右15,高30 [self.tabContainer.topAnchor constraintEqualToAnchor: _bannerScrollView.bottomAnchor constant:15], [self.tabContainer.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15], [self.tabContainer.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-15], [self.tabContainer.heightAnchor constraintEqualToConstant:30], // 按钮均分宽度 // 课程左齐容器,垂直居中,宽度1/3 [_courseBtn.leadingAnchor constraintEqualToAnchor: self.tabContainer.leadingAnchor], [_courseBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor], [_courseBtn.widthAnchor constraintEqualToAnchor: self.tabContainer.widthAnchor multiplier:1.0/3.0], // 咨询左对齐课程右侧 [_infoBtn.leadingAnchor constraintEqualToAnchor: _courseBtn.trailingAnchor], [_infoBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor], [_infoBtn.widthAnchor constraintEqualToAnchor: self.tabContainer.widthAnchor multiplier:1.0/3.0], // 活动左对齐咨询右侧 [_activityBtn.leadingAnchor constraintEqualToAnchor: _infoBtn.trailingAnchor], [_activityBtn.trailingAnchor constraintEqualToAnchor: self.tabContainer.trailingAnchor], [_activityBtn.centerYAnchor constraintEqualToAnchor: self.tabContainer.centerYAnchor], // 下划线约束 // 底部对齐容器,在button下方正中,宽30高2 [_courseUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor], [_courseUnderline.centerXAnchor constraintEqualToAnchor: _courseBtn.centerXAnchor], [_courseUnderline.widthAnchor constraintEqualToConstant:30], [_courseUnderline.heightAnchor constraintEqualToConstant:2], [_infoUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor], [_infoUnderline.centerXAnchor constraintEqualToAnchor: _infoBtn.centerXAnchor], [_infoUnderline.widthAnchor constraintEqualToConstant:30], [_infoUnderline.heightAnchor constraintEqualToConstant:2], [_activityUnderline.bottomAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor], [_activityUnderline.centerXAnchor constraintEqualToAnchor: _activityBtn.centerXAnchor], [_activityUnderline.widthAnchor constraintEqualToConstant:30], [_activityUnderline.heightAnchor constraintEqualToConstant:2], ]]; } -(void) setupConstraints{ // 登录成功提示 [NSLayoutConstraint activateConstraints:@[ [_successLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],// 水平居中 [_successLabel.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],// 垂直居中 [_successLabel.widthAnchor constraintEqualToConstant:80], [_successLabel.heightAnchor constraintEqualToConstant:30], ]]; // Logo图标 搜索框高度下垂直居中,左边距15,宽40高40 [NSLayoutConstraint activateConstraints:@[ [_logoImage.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor], [_logoImage.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15], [_logoImage.widthAnchor constraintEqualToConstant:40], [_logoImage.heightAnchor constraintEqualToConstant:40], ]]; // 搜索栏 父视图下50,logo左10,国旗左10,高30 [NSLayoutConstraint activateConstraints:@[ [_searchField.topAnchor constraintEqualToAnchor: self.view.topAnchor constant:50], [_searchField.leadingAnchor constraintEqualToAnchor: _logoImage.trailingAnchor constant:10], [_searchField.trailingAnchor constraintEqualToAnchor: _flagIcon.leadingAnchor constant:-10], [_searchField.heightAnchor constraintEqualToConstant:30], ]]; // 国旗图标 [NSLayoutConstraint activateConstraints:@[ [_flagIcon.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor], [_flagIcon.trailingAnchor constraintEqualToAnchor: _messageBtn.leadingAnchor constant:-10], [_flagIcon.widthAnchor constraintEqualToConstant:40], [_flagIcon.heightAnchor constraintEqualToConstant:40], ]]; // 消息按钮 [NSLayoutConstraint activateConstraints:@[ [_messageBtn.centerYAnchor constraintEqualToAnchor: _searchField.centerYAnchor], [_messageBtn.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-15], [_messageBtn.widthAnchor constraintEqualToConstant:40], [_messageBtn.heightAnchor constraintEqualToConstant:40], ]]; // 功能按钮(两行四列,均匀占满整行) for (int i = 0; i < _functionBtns.count; i++) { UIButton *btn = _functionBtns[i]; NSLayoutConstraint *topConstraint; NSLayoutConstraint *widthConstraint = [btn.widthAnchor constraintEqualToAnchor: self.view.widthAnchor multiplier:0.25 constant:-15]; NSLayoutConstraint *heightConstraint = [btn.heightAnchor constraintEqualToConstant:70]; if (i < 4) { topConstraint = [btn.topAnchor constraintEqualToAnchor: _searchField.bottomAnchor constant:20]; NSLayoutConstraint *leadingConstraint; if (i == 0) { leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15]; } else { leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: _functionBtns[i-1].trailingAnchor constant:0]; } [NSLayoutConstraint activateConstraints:@[topConstraint, leadingConstraint, widthConstraint, heightConstraint]]; } else { topConstraint = [btn.topAnchor constraintEqualToAnchor: _functionBtns[i-4].bottomAnchor constant:10]; NSLayoutConstraint *leadingConstraint; if (i == 4) { leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor constant:15]; } else { leadingConstraint = [btn.leadingAnchor constraintEqualToAnchor: _functionBtns[i-1].trailingAnchor constant:0]; } [NSLayoutConstraint activateConstraints:@[topConstraint, leadingConstraint, widthConstraint, heightConstraint]]; } } // 轮播图ScrollView 顶部距离最后一个功能按钮20,左右对齐父视图,高150 [NSLayoutConstraint activateConstraints:@[ [_bannerScrollView.topAnchor constraintEqualToAnchor: _functionBtns.lastObject.bottomAnchor constant:20], [_bannerScrollView.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor], [_bannerScrollView.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor], [_bannerScrollView.heightAnchor constraintEqualToConstant:150], ]]; // 轮播图PageControl,底部距离轮播图5 [NSLayoutConstraint activateConstraints:@[ [_bannerPageControl.bottomAnchor constraintEqualToAnchor: _bannerScrollView.bottomAnchor constant:-5], [_bannerPageControl.centerXAnchor constraintEqualToAnchor: self.view.centerXAnchor], ]]; // 课程列表 顶部距离标签容器20,左右对齐父视图,底部tabBar [NSLayoutConstraint activateConstraints:@[ [_courseTableView.topAnchor constraintEqualToAnchor: self.tabContainer.bottomAnchor constant:20], [_courseTableView.leadingAnchor constraintEqualToAnchor: self.view.leadingAnchor], [_courseTableView.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor], [_courseTableView.bottomAnchor constraintEqualToAnchor: _commonTabBar.topAnchor] ]]; // 红包图标 [NSLayoutConstraint activateConstraints:@[ [_redPacketIcon.bottomAnchor constraintEqualToAnchor: _courseTableView.bottomAnchor constant:-200], [_redPacketIcon.trailingAnchor constraintEqualToAnchor: self.view.trailingAnchor constant:-20], [_redPacketIcon.widthAnchor constraintEqualToConstant:60], [_redPacketIcon.heightAnchor constraintEqualToConstant:60], ]]; // 底部页面按钮 底部对齐安全区域,左右对齐父视图 [_commonTabBar.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor].active = YES; [_commonTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES; [_commonTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES; } - (void)setupData { _functionIcons = @[// 先都用logo @{@"title":@"全球指数"}, @{@"title":@"浏览历史"}, @{@"title":@"机构"}, @{@"title":@"弘历学堂"}, @{@"title":@"自选"}, @{@"title":@"商店"}, @{@"title":@"猎庄选股"}, @{@"title":@"智能机器人"} ]; _courseData = @[ @{@"cover":@"course1", @"title":@"John Lu & Robert Xu “发财线”课程回顾!", @"lecturer":@"Homily Lecturer"}, @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"kaslana"}, @{@"cover":@"course3", @"title":@"论持久战", @"lecturer":@"教员先生"} ]; } #pragma mark - 标签方法 - (void)tabButtonTapped:(UIButton *)sender { [self selectButton:sender]; // 根据标签切换 switch (sender.tag) { case 100: _courseData = @[ @{@"cover":@"course1", @"title":@"John Lu & Robert Xu “发财线”课程回顾!", @"lecturer":@"Homily Lecturer"}, @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"kaslana"}, @{@"cover":@"course3", @"title":@"论持久战", @"lecturer":@"教员先生"} ]; break; case 101: _courseData = @[ @{@"cover":@"news1", @"title":@"objective-c从入门到入土", @"lecturer":@"财经资讯"}, @{@"cover":@"news2", @"title":@"线程池核心参数详解", @"lecturer":@"财经资讯"}, @{@"cover":@"news3", @"title":@"redis集群-主从、哨兵、分片", @"lecturer":@"财经资讯"} ]; break; case 102: _courseData = @[ @{@"cover":@"event1", @"title":@"SpringCloud入门到放弃", @"lecturer":@"活动预告"}, @{@"cover":@"event2", @"title":@"孩子炒股老亏钱,多半是废了", @"lecturer":@"活动预告"}, @{@"cover":@"event3", @"title":@"mySQL:从删库到跑路", @"lecturer":@"活动预告"} ]; break; } [_courseTableView reloadData]; } - (void) pushMine { MyViewController *myViewController = [[MyViewController alloc] init]; [self.navigationController pushViewController:myViewController animated:YES]; } - (void)selectButton:(UIButton *)selectedButton { _courseBtn.selected = NO; _infoBtn.selected = NO; _activityBtn.selected = NO; _courseBtn.titleLabel.font = [UIFont systemFontOfSize:14]; _infoBtn.titleLabel.font = [UIFont systemFontOfSize:14]; _activityBtn.titleLabel.font = [UIFont systemFontOfSize:14]; _courseUnderline.hidden = YES; _infoUnderline.hidden = YES; _activityUnderline.hidden = YES; // 设置选中按钮状态 selectedButton.selected = YES; selectedButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium]; // 显示对应下划线 if (selectedButton == _courseBtn) { _courseUnderline.hidden = NO; } else if (selectedButton == _infoBtn) { _infoUnderline.hidden = NO; } else if (selectedButton == _activityBtn) { _activityUnderline.hidden = NO; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == _bannerScrollView) { CGFloat screenWidth = self.view.bounds.size.width; NSInteger currentOffsetX = scrollView.contentOffset.x; NSInteger page = currentOffsetX / screenWidth; // 首尾拼接图无缝跳转 if (page == 0) { page = 2; [scrollView setContentOffset:CGPointMake(screenWidth * page, 0) animated:NO]; } else if (page == 3) { page = 1; [scrollView setContentOffset:CGPointMake(screenWidth * page, 0) animated:NO]; } _currentBannerIndex = (page - 1) % 2; _bannerPageControl.currentPage = _currentBannerIndex; // 重启定时器 [_bannerTimer invalidate]; _bannerTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(autoScrollBanner) userInfo:nil repeats:YES]; } } - (void)autoScrollBanner { CGFloat screenWidth = self.view.bounds.size.width; // 直接基于真实索引计算下一页,避免contentOffset的延迟问题 _currentBannerIndex = (_currentBannerIndex + 1) % 2; // 0→1→0循环 NSInteger targetPage = _currentBannerIndex + 1; // 对应拼接后的页面(1/2) // 处理尾部拼接图的无缝跳转 if (targetPage > 2) { targetPage = 1; [_bannerScrollView setContentOffset:CGPointMake(screenWidth * targetPage, 0) animated:NO]; } [_bannerScrollView setContentOffset:CGPointMake(screenWidth * targetPage, 0) animated:YES]; _bannerPageControl.currentPage = _currentBannerIndex; } - (void) pushQuotation { QuotationViewController *quotationViewController = [[QuotationViewController alloc] init]; [self.navigationController pushViewController:quotationViewController animated:YES]; } - (void)tabBarDidSelectIndex:(NSInteger)index { if (index == 3) { [self pushMine]; } else if (index == 0) { } else if (index == 1) { [self pushQuotation]; } else if (index == 2) { // 自选 } } #pragma mark - 视图即将消失 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_bannerTimer invalidate];// 销毁定时器 _bannerTimer = nil; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:0.3 animations:^{ self->_successLabel.alpha = 0; } completion:^(BOOL finished) { [self->_successLabel removeFromSuperview]; }]; }); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _courseData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor blackColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; for (UIView *subview in cell.contentView.subviews) { [subview removeFromSuperview]; } NSDictionary *course = _courseData[indexPath.row]; UIImageView *coverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:course[@"cover"]]]; coverView.contentMode = UIViewContentModeScaleAspectFill; coverView.clipsToBounds = YES; coverView.frame = CGRectMake(15, 10, 140, 100); [cell.contentView addSubview:coverView]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 10, 220, 60)]; titleLabel.text = course[@"title"]; titleLabel.textColor = [UIColor whiteColor]; titleLabel.font = [UIFont systemFontOfSize:14]; titleLabel.numberOfLines = 3; [cell.contentView addSubview:titleLabel]; UILabel *lecturerLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 75, 120, 30)]; lecturerLabel.text = course[@"lecturer"]; lecturerLabel.textColor = [UIColor lightGrayColor]; lecturerLabel.font = [UIFont systemFontOfSize:14]; [cell.contentView addSubview:lecturerLabel]; UIView *bottomLine = [[UIView alloc] init]; bottomLine.backgroundColor = [UIColor lightGrayColor]; bottomLine.translatesAutoresizingMaskIntoConstraints = NO; [cell.contentView addSubview:bottomLine]; [NSLayoutConstraint activateConstraints:@[ [bottomLine.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:20], // 左边距15 [bottomLine.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor constant:-20], // 右边距15 [bottomLine.bottomAnchor constraintEqualToAnchor:cell.contentView.bottomAnchor], [bottomLine.heightAnchor constraintEqualToConstant:0.2] ]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 120; } @end