THScrollChooseView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // THScrollChooseView.m
  3. // THChooseTool
  4. #define THScreenW [UIScreen mainScreen].bounds.size.width
  5. #define THScreenH [UIScreen mainScreen].bounds.size.height
  6. #define THWParam [UIScreen mainScreen].bounds.size.width/375.0f
  7. #define THfloat(a) a*THWParam
  8. #import "THScrollChooseView.h"
  9. @interface THScrollChooseView()<UIPickerViewDataSource, UIPickerViewDelegate>
  10. @property (strong, nonatomic) UIPickerView *pickerView;
  11. @property (strong, nonatomic) UIView *bottomView;
  12. /**
  13. 取消按钮
  14. */
  15. @property (strong, nonatomic) UIButton *cancelButton;
  16. /**
  17. 确定按钮
  18. */
  19. @property (strong, nonatomic) UIButton *confirmButton;
  20. /**
  21. 选中数据是第几条
  22. */
  23. @property (assign, nonatomic) NSInteger selectedValue;
  24. /**
  25. 数组
  26. */
  27. @property (strong, nonatomic) NSArray *questionArray;
  28. /**
  29. 默认的值
  30. */
  31. @property (copy, nonatomic) NSString *defaultDesc;
  32. @end
  33. @implementation THScrollChooseView
  34. static NSInteger recordRowOfQuestion;
  35. - (instancetype)initWithQuestionArray:(NSArray *)questionArray withDefaultDesc:(NSString *)defaultDesc {
  36. if (self = [super init]) {
  37. self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
  38. self.frame = CGRectMake(0, 0, THScreenW, THScreenH);
  39. UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-260, CGRectGetWidth(self.frame), 260)];
  40. whiteView.backgroundColor = [UIColor whiteColor];
  41. [self addSubview:whiteView];
  42. //按钮所在区域
  43. UIView *viewBg = [[UIView alloc] initWithFrame:CGRectMake(0,0,THScreenW,THfloat(45))];
  44. [viewBg setBackgroundColor:[UIColor whiteColor]];
  45. [whiteView addSubview:viewBg];
  46. //创建取消 确定按钮
  47. UIButton *cannel = [UIButton buttonWithType:UIButtonTypeCustom];
  48. cannel.frame = CGRectMake(THfloat(20), 0, THfloat(50), THfloat(45));
  49. [cannel setTitle:NSLocalizedString(@"取消", nil) forState:0];
  50. cannel.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
  51. [cannel setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  52. cannel.tag = 1;
  53. [cannel addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  54. [viewBg addSubview:cannel];
  55. UIButton *confirm = [UIButton buttonWithType:UIButtonTypeCustom];
  56. confirm.frame = CGRectMake(THScreenW - THfloat(70), 0, THfloat(50), THfloat(45));
  57. [confirm setTitle:NSLocalizedString(@"确定", nil) forState:0];
  58. confirm.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
  59. [confirm setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  60. confirm.tag = 2;
  61. [confirm addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  62. [viewBg addSubview:confirm];
  63. self.questionArray = questionArray;
  64. self.defaultDesc = defaultDesc;
  65. [whiteView addSubview:self.pickerView];
  66. //[self.pickerView reloadAllComponents];
  67. }
  68. return self;
  69. }
  70. #pragma mark - action
  71. - (void)cancelButtonAction:(UIButton *)button {
  72. [self dismissView];
  73. }
  74. - (void)confirmButtonAction:(UIButton *)button {
  75. if (self.selectedValue >= self.questionArray.count) { //防止切换的时候 会出现问题
  76. self.selectedValue = 0; //啥都不选,就点击OK //有默认值的话,一定是小雨self.questionArray.count
  77. }
  78. self.confirmBlock(self.selectedValue);
  79. [self dismissView];
  80. }
  81. - (void)showView{
  82. [[UIApplication sharedApplication].keyWindow addSubview:self];
  83. self.alpha = 0;
  84. [UIView animateWithDuration:0.3 animations:^{
  85. self.alpha = 1;
  86. } completion:^(BOOL finished) {
  87. }];
  88. }
  89. - (void)dismissView{
  90. [UIView animateWithDuration:0.3 animations:^{
  91. self.alpha = 0;
  92. } completion:^(BOOL finished) {
  93. [self removeFromSuperview];
  94. }];
  95. }
  96. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  97. [self dismissView];
  98. }
  99. #pragma mark - pickerView 代理方法
  100. // 列数
  101. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  102. return 1;
  103. }
  104. // 行数
  105. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  106. return self.questionArray.count;
  107. }
  108. -(CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component {
  109. return THfloat(35);
  110. }
  111. // 显示什么
  112. - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  113. return self.questionArray[row];
  114. }
  115. // 选中时
  116. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  117. self.selectedValue = row;
  118. }
  119. - (UIPickerView *)pickerView {
  120. if (!_pickerView) {
  121. _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, THfloat(45), THScreenW, THfloat(180))];
  122. _pickerView.backgroundColor = [UIColor clearColor];
  123. _pickerView.dataSource = self;
  124. _pickerView.delegate = self;
  125. // _pickerView 初始化显示的值
  126. if (self.defaultDesc.length == 0) {
  127. //无默认值时显示第一个
  128. [_pickerView selectRow:0 inComponent:0 animated:YES];
  129. }else {
  130. //有默认值时显示默认值
  131. recordRowOfQuestion = [self rowOfQuestionWithName:self.defaultDesc];
  132. [_pickerView selectRow:recordRowOfQuestion inComponent:0 animated:YES];
  133. self.selectedValue = recordRowOfQuestion;
  134. }
  135. }
  136. return _pickerView;
  137. }
  138. - (NSInteger)rowOfQuestionWithName:(NSString *)questionName{
  139. NSInteger row = 0;
  140. for (NSString *str in self.questionArray) {
  141. if ([str containsString:questionName]) {
  142. NSLog(@"row = %ld",row);
  143. return row;
  144. }
  145. row++;
  146. }
  147. return row;
  148. }
  149. @end