RQExamProbabilityViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // RQExamProbabilityViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/7/29.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQExamProbabilityViewController.h"
  9. NSString * const RQExamProbabilityUpdateNotification = @"RQExamProbabilityUpdateNotification";
  10. NSString * const RQExamResultListItemKey = @"RQExamResultListItemKey";
  11. NSString * const RQExamResultListSubjectKey = @"RQExamResultListSubjectKey";
  12. @interface RQExamProbabilityViewController ()
  13. /// viewModel
  14. @property (nonatomic, readonly, strong) RQExamProbabilityViewModel *viewModel;
  15. @property (nonatomic, readwrite, strong) QMUINavigationBarScrollingAnimator *navigationAnimator;
  16. @property (nonatomic, readwrite, strong) QMUIPopupMenuView *popupAtBarButtonItem;
  17. @property (nonatomic, readwrite, strong) UIBarButtonItem *rightBarButtonItem;
  18. @property (nonatomic, readwrite, strong) QMUIButton *btn;
  19. @property (nonatomic, readwrite, assign) BOOL isSubjectOne;
  20. @end
  21. @implementation RQExamProbabilityViewController
  22. @dynamic viewModel;
  23. #pragma mark - SystemMethod
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. @weakify(self)
  27. self.isSubjectOne = (RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne);
  28. self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
  29. // code here
  30. self.navigationAnimator = [[QMUINavigationBarScrollingAnimator alloc] init];
  31. self.navigationAnimator.scrollView = self.tableView;// 指定要关联的 scrollView
  32. self.navigationAnimator.offsetYToStartAnimation = 30;// 设置滚动的起点,值即表示在默认停靠的位置往下滚动多少距离后即触发动画,默认是 0
  33. self.navigationAnimator.distanceToStopAnimation = 64;// 设置从起点开始滚动多长的距离达到终点
  34. self.navigationAnimator.backgroundImageBlock = ^UIImage * _Nonnull(QMUINavigationBarScrollingAnimator * _Nonnull animator, float progress) {
  35. return [[UIImage qmui_imageWithColor:RQColorFromHexString(@"#2C2D3C") size:CGSizeMake(RQ_SCREEN_WIDTH, RQ_APPLICATION_NAV_BAR_HEIGHT) cornerRadius:0] qmui_imageWithAlpha:progress];
  36. };
  37. self.navigationAnimator.shadowImageBlock = ^UIImage * _Nonnull(QMUINavigationBarScrollingAnimator * _Nonnull animator, float progress) {
  38. return [NavBarShadowImage qmui_imageWithAlpha:progress];
  39. };
  40. self.navigationAnimator.tintColorBlock = ^UIColor * _Nonnull(QMUINavigationBarScrollingAnimator * _Nonnull animator, float progress) {
  41. return [UIColor qmui_colorFromColor:UIColorWhite toColor:UIColorWhite progress:progress];
  42. };
  43. self.navigationAnimator.titleViewTintColorBlock = self.navigationAnimator.tintColorBlock;
  44. self.navigationAnimator.statusbarStyleBlock = ^UIStatusBarStyle(QMUINavigationBarScrollingAnimator * _Nonnull animator, float progress) {
  45. return progress < .25 ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
  46. };
  47. }
  48. - (void)handleRightBarButtonItemEvent {
  49. if (self.popupAtBarButtonItem.isShowing) {
  50. [self.popupAtBarButtonItem hideWithAnimated:YES];
  51. } else {
  52. // 相对于右上角的按钮布局
  53. self.popupAtBarButtonItem.sourceBarItem = self.navigationItem.rightBarButtonItem;
  54. [self.popupAtBarButtonItem showWithAnimated:YES];
  55. }
  56. }
  57. #pragma mark - OverrideMethods
  58. /// 配置tableView的区域
  59. - (UIEdgeInsets)contentInset {
  60. return UIEdgeInsetsMake(0, 0, RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT, 0);
  61. }
  62. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  63. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  64. RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  65. return [NSClassFromString(itemViewModel.itemClassName) cellWithTableView:tableView];
  66. }
  67. - (void)configureCell:(RQCommonCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  68. [cell bindViewModel:object];
  69. }
  70. - (UIStatusBarStyle)preferredStatusBarStyle {
  71. // 需要手动调用 navigationAnimator.statusbarStyleBlock 来告诉系统状态栏的变化
  72. if (self.navigationAnimator) {
  73. return self.navigationAnimator.statusbarStyleBlock(self.navigationAnimator, self.navigationAnimator.progress);
  74. }
  75. return [super preferredStatusBarStyle];
  76. }
  77. // 建议配合 QMUINavigationControllerAppearanceDelegate 控制不同界面切换时的 navigationBar 样式,否则需自己在 viewWillAppear:、viewWillDisappear: 里控制
  78. #pragma mark - <QMUINavigationControllerAppearanceDelegate>
  79. - (UIImage *)qmui_navigationBarBackgroundImage {
  80. return self.navigationAnimator.backgroundImageBlock(self.navigationAnimator, self.navigationAnimator.progress);
  81. }
  82. - (UIImage *)qmui_navigationBarShadowImage {
  83. return self.navigationAnimator.shadowImageBlock(self.navigationAnimator, self.navigationAnimator.progress);
  84. }
  85. - (UIColor *)qmui_navigationBarTintColor {
  86. return self.navigationAnimator.tintColorBlock(self.navigationAnimator, self.navigationAnimator.progress);
  87. }
  88. - (UIColor *)qmui_titleViewTintColor {
  89. return [self qmui_navigationBarTintColor];
  90. }
  91. #pragma mark - <QMUICustomNavigationBarTransitionDelegate>
  92. // 为了展示接口的使用,QMUI Demo 没有打开配置表的 AutomaticCustomNavigationBarTransitionStyle,因此当 navigationBar 样式与默认样式不同时,需要手动在 customNavigationBarTransitionKey 里返回一个与其他界面不相同的值,这样才能使用自定义的 navigationBar 转场样式
  93. - (NSString *)customNavigationBarTransitionKey {
  94. return self.navigationAnimator.progress >= 1 ? nil : @"progress";
  95. }
  96. - (QMUIPopupMenuView *)popupAtBarButtonItem {
  97. if (!_popupAtBarButtonItem) {
  98. @weakify(self)
  99. _popupAtBarButtonItem = [[QMUIPopupMenuView alloc] init];
  100. _popupAtBarButtonItem.automaticallyHidesWhenUserTap = YES;// 点击空白地方消失浮层
  101. _popupAtBarButtonItem.maximumWidth = RQ_SCREEN_WIDTH / 2.f;
  102. _popupAtBarButtonItem.shouldShowItemSeparator = YES;
  103. _popupAtBarButtonItem.tintColor = RQ_MAIN_BACKGROUNDCOLOR;
  104. _popupAtBarButtonItem.items = @[[QMUIPopupMenuButtonItem itemWithImage:nil title:@"科目一" handler:^(QMUIPopupMenuButtonItem * _Nonnull aItem) {
  105. @strongify(self)
  106. [self.btn setTitleNormal:aItem.title];
  107. [RQNotificationCenter postNotificationName:RQExamProbabilityUpdateNotification object:@{
  108. RQExamResultListItemKey : RQ_COMMON_MANAGER.examResultOneListArr,
  109. RQExamResultListSubjectKey : @(RQHomePageSubjectType_SubjectOne),
  110. }];
  111. [aItem.menuView hideWithAnimated:YES];
  112. }],
  113. [QMUIPopupMenuButtonItem itemWithImage:nil title:@"科目四" handler:^(QMUIPopupMenuButtonItem * _Nonnull aItem) {
  114. @strongify(self)
  115. [self.btn setTitleNormal:aItem.title];
  116. [RQNotificationCenter postNotificationName:RQExamProbabilityUpdateNotification object:@{
  117. RQExamResultListItemKey : RQ_COMMON_MANAGER.examResultFourListArr,
  118. RQExamResultListSubjectKey : @(RQHomePageSubjectType_SubjectFour),
  119. }];
  120. [aItem.menuView hideWithAnimated:YES];
  121. }],
  122. ];
  123. }
  124. return _popupAtBarButtonItem;
  125. }
  126. - (QMUIButton *)btn {
  127. if (!_btn) {
  128. _btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  129. [_btn setTitle:self.isSubjectOne? @"科目一" : @"科目四" forState:UIControlStateNormal];
  130. [_btn setImage:RQImageNamed(@"向下") withTitle:self.isSubjectOne? @"科目一" : @"科目四" textColor:RQ_MAIN_BACKGROUNDCOLOR Font:16 fotState:UIControlStateNormal];
  131. [_btn addTarget:self action:@selector(handleRightBarButtonItemEvent) forControlEvents:UIControlEventTouchUpInside];
  132. _btn.imagePosition = QMUIButtonImagePositionRight;
  133. _btn.spacingBetweenImageAndTitle = 8.f;
  134. }
  135. return _btn;
  136. }
  137. - (UIBarButtonItem *)rightBarButtonItem {
  138. if (!_rightBarButtonItem) {
  139. _rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btn];
  140. }
  141. return _rightBarButtonItem;
  142. }
  143. @end