123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- //
- // YMPickerPopupView.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/2/11.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "YMPickerPopupView.h"
- @interface YMPickerPopupView ()<UIPickerViewDelegate,UIPickerViewDataSource>
- /** 选择器VM*/
- @property (nonatomic, strong) YMPickerViewModel *viewModel;
- /** 标题标签*/
- @property (nonatomic, strong) UILabel *titleLb;
- /** 取消按钮*/
- @property (nonatomic, strong) UIButton *cancelBtn;
- /** 确认按钮*/
- @property (nonatomic, strong) UIButton *confirmBtn;
- /** 选择器*/
- @property (nonatomic, strong) UIPickerView *pickerView;
- /** 当前选中数据*/
- @property (nonatomic, strong) NSArray <NSMutableDictionary*>*currentSelectedDataArray;
- @end
- @implementation YMPickerPopupView
- - (void)ym_setupViews{
- self.frame = CGRectMake(0, 0, kFrameWidth, 250);
-
- self.backgroundColor = HexColorFromRGB(0xFFFFFF);
- self.titleText = @"请选择选项";
- self.titleColor = HexColorFromRGB(0x000000);
-
- self.titleFont = LCFont(13);
- self.cancelBgColor = [UIColor clearColor];
- self.confirmBgColor = [UIColor clearColor];
-
- self.cancelTitleColor = HexColorFromRGB(0x333333);
- self.confirmTitleColor = HexColorFromRGB(0x333333);
-
- self.cancelFont = LCFont(14);
- self.confirmFont = LCFont(14);
-
- self.cancelTitle = @"取消";
- self.confirmTitle = @"确定";
-
- self.cancelRadius = 10;
- self.confirmRadius = 10;
-
- self.cancelBorderColor = [UIColor clearColor];
- self.confirmBorderColor = [UIColor clearColor];
-
- self.cancelBorderWidth = 0;
- self.confirmBorderWidth = 0;
-
-
- [self addSubview:self.cancelBtn];
- [self addSubview:self.titleLb];
- [self addSubview:self.confirmBtn];
- [self addSubview:self.pickerView];
-
- [self setNeedsUpdateConstraints];
- [self updateConstraintsIfNeeded];
- }
- - (void)updateConstraints{
-
- [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.left.equalTo(self).offset(adapt(10));
- make.width.mas_equalTo(adapt(60));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.left.equalTo(self.cancelBtn.mas_right).offset(adapt(10));
- make.right.equalTo(self.confirmBtn.mas_left).offset(adapt(-10));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.right.equalTo(self).offset(adapt(-10));
- make.width.mas_equalTo(adapt(60));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLb.mas_bottom);
- make.left.equalTo(self);
- make.right.equalTo(self);
- make.bottom.equalTo(self).offset(adapt(-15));
- }];
-
- [super updateConstraints];
- }
- - (void)ym_bindViewModel:(YMPickerViewModel *)viewModel{
- if (!viewModel) {
- return;
- }
-
- _viewModel = viewModel;
-
- [self.pickerView reloadAllComponents];
- for (int i = 0; i < self.viewModel.componentDataSource.count; i++) {
- //每次显示picker的时候都选中第一行
- [self.pickerView selectRow:0 inComponent:i animated:YES];
- //实现默认选中第一行的点击方法,将第一行的title传给控制器
- [self pickerView:self.pickerView didSelectRow:0 inComponent:i];
- }
- }
- //返回UIPickerView中Component列的宽度
- - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
- return self.viewModel.componentDataSource.count <= 1 ? self.frame.size.width : self.viewModel.componentDataSource[component].componentWidth;
- }
-
- //返回UIPickerView中Component列中每行的高度
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
- return 50;
- }
- //该方法返回值决定该控件包含多少列
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
- return self.viewModel.componentDataSource.count;
- }
- //该方法的返回值决定该控件指定列包含多少个列表项
- - (NSInteger)pickerView:(UIPickerView *)pickerView
- numberOfRowsInComponent:(NSInteger)component{
- return self.viewModel.componentDataSource[component].rowDataSource.count;
- }
- // UIPickerView中指定列和列表项上显示的标题
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
- (NSInteger)row forComponent:(NSInteger)component{
- return self.viewModel.componentDataSource[component].rowDataSource[row].rowName;
- }
- // 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
- self.viewModel.componentDataSource[component].currentSelectedSectionIndex = component;
- self.viewModel.componentDataSource[component].currentSelectedRowIndex = row;
- self.viewModel.componentDataSource[component].currentSelectedValue = self.viewModel.componentDataSource[component].rowDataSource[row].rowName;
- }
- - (void)setTitleText:(NSString *)titleText{
- _titleText = titleText;
- self.titleLb.text = titleText;
- }
- - (void)setCancelBgColor:(UIColor *)cancelBgColor{
- _cancelBgColor = cancelBgColor;
- _cancelBtn.backgroundColor = cancelBgColor;
- }
- - (void)setConfirmBgColor:(UIColor *)confirmBgColor{
- _confirmBgColor = confirmBgColor;
- self.confirmBtn.backgroundColor = confirmBgColor;
- }
- - (void)setCancelTitleColor:(UIColor *)cancelTitleColor{
- _cancelTitleColor = cancelTitleColor;
- [self.cancelBtn setTitleColor:cancelTitleColor forState:UIControlStateNormal];
- }
- - (void)setConfirmTitleColor:(UIColor *)confirmTitleColor{
- _confirmTitleColor = confirmTitleColor;
- [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
- }
- - (void)setCancelFont:(UIFont *)cancelFont{
- _cancelFont = cancelFont;
- self.cancelBtn.titleLabel.font = cancelFont;
- }
- - (void)setConfirmFont:(UIFont *)confirmFont{
- _confirmFont = confirmFont;
- self.confirmBtn.titleLabel.font = confirmFont;
- }
- - (void)setCancelTitle:(NSString *)cancelTitle{
- _cancelTitle = cancelTitle;
- [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
- }
- - (void)setConfirmTitle:(NSString *)confirmTitle{
- _confirmTitle = confirmTitle;
- [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
- }
- - (void)setCancelRadius:(CGFloat)cancelRadius{
- _cancelRadius = cancelRadius;
- self.cancelBtn.layer.cornerRadius = cancelRadius;
- }
- - (void)setConfirmRadius:(CGFloat)confirmRadius{
- _confirmRadius = confirmRadius;
- self.confirmBtn.layer.cornerRadius = confirmRadius;
- }
- - (void)setCancelBorderColor:(UIColor *)cancelBorderColor{
- _cancelBorderColor = cancelBorderColor;
- self.cancelBtn.layer.borderColor = cancelBorderColor.CGColor;
- }
- - (void)setConfirmBorderColor:(UIColor *)confirmBorderColor{
- _confirmBorderColor = confirmBorderColor;
- self.confirmBtn.layer.borderColor = confirmBorderColor.CGColor;
- }
- - (void)setCancelBorderWidth:(CGFloat)cancelBorderWidth{
- _cancelBorderWidth = cancelBorderWidth;
- self.cancelBtn.layer.borderWidth = cancelBorderWidth;
- }
- - (void)setConfirmBorderWidth:(CGFloat)confirmBorderWidth{
- _confirmBorderWidth = confirmBorderWidth;
- self.confirmBtn.layer.borderWidth = confirmBorderWidth;
- }
- - (UILabel *)titleLb{
- if (!_titleLb) {
- _titleLb = [[UILabel alloc]init];
- _titleLb.textAlignment = NSTextAlignmentCenter;
- _titleLb.textColor = self.titleColor;
- _titleLb.font = self.titleFont;
- _titleLb.text = self.titleText;
- }
- return _titleLb;
- }
- - (UIButton *)cancelBtn {
- if(!_cancelBtn){
- _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _cancelBtn.backgroundColor = self.cancelBgColor;
- _cancelBtn.titleLabel.font = self.cancelFont;
- [_cancelBtn setTitleColor:self.cancelTitleColor forState:UIControlStateNormal];
- [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
- _cancelBtn.layer.cornerRadius = self.cancelRadius;
- _cancelBtn.layer.borderWidth = self.cancelBorderWidth;
- _cancelBtn.layer.borderColor = self.cancelBorderColor.CGColor;
- WS(weakSelf)
- [[[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- if (weakSelf.buttonBlock) {
- weakSelf.buttonBlock(NO, @[]);
- }
- }];
- }
- return _cancelBtn;
- }
- - (UIButton *)confirmBtn {
- if(!_confirmBtn){
- _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _confirmBtn.backgroundColor = self.confirmBgColor;
- _confirmBtn.titleLabel.font = self.confirmFont;
- [_confirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal];
- [_confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
- _confirmBtn.layer.cornerRadius = self.confirmRadius;
- _confirmBtn.layer.borderWidth = self.confirmBorderWidth;
- _confirmBtn.layer.borderColor = self.confirmBorderColor.CGColor;
- WS(weakSelf)
- [[[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- if (weakSelf.buttonBlock) {
- weakSelf.currentSelectedDataArray = [weakSelf.viewModel.componentDataSource.rac_sequence map:^(YMPickerComponentViewModel *_Nullable componentModel) {
- NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
- [mDic setObject:componentModel.componentName forKey:@"componentName"];
- [mDic setObject:@(componentModel.currentSelectedSectionIndex) forKey:@"currentSelectedSectionIndex"];
- [mDic setObject:@(componentModel.currentSelectedRowIndex) forKey:@"currentSelectedRowIndex"];
- [mDic setObject:componentModel.currentSelectedValue forKey:@"currentSelectedValue"];
- return mDic;
- }].array;
-
- weakSelf.buttonBlock(YES, weakSelf.currentSelectedDataArray);
- }
- }];
- }
- return _confirmBtn;
- }
- - (UIPickerView *)pickerView{
- if (!_pickerView) {
- _pickerView = [[UIPickerView alloc]init];
- _pickerView.delegate = self;
- _pickerView.dataSource = self;
- _pickerView.showsSelectionIndicator = YES;
- }
- return _pickerView;
- }
- - (NSArray <NSMutableDictionary*>*)currentSelectedDataArray{
- if (!_currentSelectedDataArray) {
- _currentSelectedDataArray = [NSArray array];
- }
- return _currentSelectedDataArray;
- }
- @end
- @interface YMPickerViewModel ()
- /** 行列表 */
- @property (nonatomic, strong, readwrite) NSArray <YMPickerComponentViewModel*>*componentDataSource;
- @end
- @implementation YMPickerViewModel
- - (void)ym_initialize{
- [super ym_initialize];
- if([[self.params allKeys] containsObject:@"componentDataSource"]){
- self.componentDataSource = [[self.params arrayValueForKey:@"componentDataSource" defaultValue:@[]].rac_sequence map:^(id _Nullable componentModel) {
- YMPickerComponentViewModel *viewModel = [[YMPickerComponentViewModel alloc]initWithParams:componentModel];
- return viewModel;
- }].array;
- }
- }
- @end
- @interface YMPickerComponentViewModel ()
- /** 分组Id */
- @property (nonatomic, copy, readwrite) NSString *componentId;
- /** 分组名 */
- @property (nonatomic, copy, readwrite) NSString *componentName;
- /** 分组宽度 */
- @property (nonatomic, assign, readwrite) CGFloat componentWidth;
- /** 行列表 */
- @property (nonatomic, strong, readwrite) NSArray *rowDataSource;
- @end
- @implementation YMPickerComponentViewModel
- - (void)ym_initialize{
- [super ym_initialize];
- if([[self.params allKeys] containsObject:@"componentId"]){
- self.componentId = [self.params stringValueForKey:@"componentId" defaultValue:@""];
- }
- if([[self.params allKeys] containsObject:@"componentName"]){
- self.componentName = [self.params stringValueForKey:@"componentName" defaultValue:@""];
- }
-
- self.componentWidth = [self.params floatValueForKey:@"componentWidth" defaultValue:100.0f];
-
- if([[self.params allKeys] containsObject:@"rowDataSource"]){
- self.rowDataSource = [[self.params arrayValueForKey:@"rowDataSource" defaultValue:@[]].rac_sequence map:^(id _Nullable rowModel) {
- YMPickerRowViewModel *viewModel = [[YMPickerRowViewModel alloc]initWithParams:rowModel];
- return viewModel;
- }].array;
- }
- }
- @end
- @interface YMPickerRowViewModel ()
- /** 分组Id */
- @property (nonatomic, copy, readwrite) NSString *componentId;
- /** 行Id */
- @property (nonatomic, copy, readwrite) NSString *rowId;
- /** 标题名 */
- @property (nonatomic, copy, readwrite) NSString *rowName;
- @end
- @implementation YMPickerRowViewModel
- - (void)ym_initialize{
- [super ym_initialize];
- if([[self.params allKeys] containsObject:@"componentId"]){
- self.componentId = [self.params stringValueForKey:@"componentId" defaultValue:@""];
- }
- if([[self.params allKeys] containsObject:@"rowId"]){
- self.rowId = [self.params stringValueForKey:@"rowId" defaultValue:@""];
- }
- if([[self.params allKeys] containsObject:@"rowName"]){
- self.rowName = [self.params stringValueForKey:@"rowName" defaultValue:@""];
- }
- }
- @end
|