| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- //
- // CategoryPickerView.m
- // BuguLive
- //
- // Created by qitewei on 2025/8/20.
- // Copyright © 2025 xfg. All rights reserved.
- //
- #import "CategoryPickerView.h"
- #import "UIView+Extention.h"
- #import <Masonry/Masonry.h>
- @interface CategoryPickerView () <UITableViewDataSource, UITableViewDelegate>
- @property (nonatomic, strong) UIView *backgroundView;
- @property (nonatomic, strong) UIView *containerView;
- @property (nonatomic, strong) UITableView *tableView;
- @end
- @implementation CategoryPickerView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- self.backgroundColor = [UIColor clearColor];
-
- // 背景遮罩
- [self addSubview:self.backgroundView];
-
- // 容器视图
- [self addSubview:self.containerView];
-
- // 表格视图
- [self.containerView addSubview:self.tableView];
-
- [self setupConstraints];
- }
- - (void)setupConstraints {
- [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self);
- }];
-
- [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(self);
- make.height.mas_equalTo(kRealValue(300));
- }];
-
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.containerView);
- }];
- }
- - (void)showInView:(UIView *)parentView fromRect:(CGRect)rect {
- self.frame = parentView.bounds;
- [parentView addSubview:self];
-
- // 初始状态
- self.alpha = 0;
- self.containerView.transform = CGAffineTransformMakeTranslation(0, kRealValue(300));
-
- // 动画显示
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 1;
- self.containerView.transform = CGAffineTransformIdentity;
- }];
- }
- - (void)hide {
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 0;
- self.containerView.transform = CGAffineTransformMakeTranslation(0, kRealValue(300));
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- - (void)backgroundTapped:(UITapGestureRecognizer *)gesture {
- [self hide];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.categories.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *cellIdentifier = @"CategoryCell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.backgroundColor = [UIColor whiteColor];
- cell.textLabel.font = [UIFont systemFontOfSize:16];
- cell.textLabel.textColor = [UIColor blackColor];
- }
-
- IndustryCategoryModel *category = self.categories[indexPath.row];
- cell.textLabel.text = category.name;
-
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- IndustryCategoryModel *selectedCategory = self.categories[indexPath.row];
- if ([self.delegate respondsToSelector:@selector(categoryPickerView:didSelectCategory:)]) {
- [self.delegate categoryPickerView:self didSelectCategory:selectedCategory];
- }
-
- [self hide];
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return kRealValue(50);
- }
- #pragma mark - Lazy Loading
- - (UIView *)backgroundView {
- if (!_backgroundView) {
- _backgroundView = [[UIView alloc] init];
- _backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
-
- UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
- [_backgroundView addGestureRecognizer:tapGesture];
- }
- return _backgroundView;
- }
- - (UIView *)containerView {
- if (!_containerView) {
- _containerView = [[UIView alloc] init];
- _containerView.backgroundColor = [UIColor whiteColor];
- _containerView.layer.cornerRadius = kRealValue(12);
- _containerView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
- _containerView.clipsToBounds = YES;
- }
- return _containerView;
- }
- - (UITableView *)tableView {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.backgroundColor = [UIColor whiteColor];
- _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
- _tableView.separatorColor = [UIColor colorWithHexString:@"#E5E5E5"];
- _tableView.showsVerticalScrollIndicator = YES;
- _tableView.bounces = YES;
- }
- return _tableView;
- }
- @end
|