CategoryPickerView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // CategoryPickerView.m
  3. // BuguLive
  4. //
  5. // Created by qitewei on 2025/8/20.
  6. // Copyright © 2025 xfg. All rights reserved.
  7. //
  8. #import "CategoryPickerView.h"
  9. #import "UIView+Extention.h"
  10. #import <Masonry/Masonry.h>
  11. @interface CategoryPickerView () <UITableViewDataSource, UITableViewDelegate>
  12. @property (nonatomic, strong) UIView *backgroundView;
  13. @property (nonatomic, strong) UIView *containerView;
  14. @property (nonatomic, strong) UITableView *tableView;
  15. @end
  16. @implementation CategoryPickerView
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self setupUI];
  21. }
  22. return self;
  23. }
  24. - (void)setupUI {
  25. self.backgroundColor = [UIColor clearColor];
  26. // 背景遮罩
  27. [self addSubview:self.backgroundView];
  28. // 容器视图
  29. [self addSubview:self.containerView];
  30. // 表格视图
  31. [self.containerView addSubview:self.tableView];
  32. [self setupConstraints];
  33. }
  34. - (void)setupConstraints {
  35. [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.edges.equalTo(self);
  37. }];
  38. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.right.bottom.equalTo(self);
  40. make.height.mas_equalTo(kRealValue(300));
  41. }];
  42. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.edges.equalTo(self.containerView);
  44. }];
  45. }
  46. - (void)showInView:(UIView *)parentView fromRect:(CGRect)rect {
  47. self.frame = parentView.bounds;
  48. [parentView addSubview:self];
  49. // 初始状态
  50. self.alpha = 0;
  51. self.containerView.transform = CGAffineTransformMakeTranslation(0, kRealValue(300));
  52. // 动画显示
  53. [UIView animateWithDuration:0.3 animations:^{
  54. self.alpha = 1;
  55. self.containerView.transform = CGAffineTransformIdentity;
  56. }];
  57. }
  58. - (void)hide {
  59. [UIView animateWithDuration:0.3 animations:^{
  60. self.alpha = 0;
  61. self.containerView.transform = CGAffineTransformMakeTranslation(0, kRealValue(300));
  62. } completion:^(BOOL finished) {
  63. [self removeFromSuperview];
  64. }];
  65. }
  66. - (void)backgroundTapped:(UITapGestureRecognizer *)gesture {
  67. [self hide];
  68. }
  69. #pragma mark - UITableViewDataSource
  70. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  71. return self.categories.count;
  72. }
  73. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  74. static NSString *cellIdentifier = @"CategoryCell";
  75. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  76. if (!cell) {
  77. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  78. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  79. cell.backgroundColor = [UIColor whiteColor];
  80. cell.textLabel.font = [UIFont systemFontOfSize:16];
  81. cell.textLabel.textColor = [UIColor blackColor];
  82. }
  83. IndustryCategoryModel *category = self.categories[indexPath.row];
  84. cell.textLabel.text = category.name;
  85. return cell;
  86. }
  87. #pragma mark - UITableViewDelegate
  88. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  89. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  90. IndustryCategoryModel *selectedCategory = self.categories[indexPath.row];
  91. if ([self.delegate respondsToSelector:@selector(categoryPickerView:didSelectCategory:)]) {
  92. [self.delegate categoryPickerView:self didSelectCategory:selectedCategory];
  93. }
  94. [self hide];
  95. }
  96. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  97. return kRealValue(50);
  98. }
  99. #pragma mark - Lazy Loading
  100. - (UIView *)backgroundView {
  101. if (!_backgroundView) {
  102. _backgroundView = [[UIView alloc] init];
  103. _backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
  104. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
  105. [_backgroundView addGestureRecognizer:tapGesture];
  106. }
  107. return _backgroundView;
  108. }
  109. - (UIView *)containerView {
  110. if (!_containerView) {
  111. _containerView = [[UIView alloc] init];
  112. _containerView.backgroundColor = [UIColor whiteColor];
  113. _containerView.layer.cornerRadius = kRealValue(12);
  114. _containerView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
  115. _containerView.clipsToBounds = YES;
  116. }
  117. return _containerView;
  118. }
  119. - (UITableView *)tableView {
  120. if (!_tableView) {
  121. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  122. _tableView.dataSource = self;
  123. _tableView.delegate = self;
  124. _tableView.backgroundColor = [UIColor whiteColor];
  125. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  126. _tableView.separatorColor = [UIColor colorWithHexString:@"#E5E5E5"];
  127. _tableView.showsVerticalScrollIndicator = YES;
  128. _tableView.bounces = YES;
  129. }
  130. return _tableView;
  131. }
  132. @end