THScrollChooseView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(@"Cancel", nil) forState:0];
  50. cannel.titleLabel.font = [UIFont systemFontOfSize:THfloat(16)];
  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(@"OK", nil) forState:0];
  58. confirm.titleLabel.font = [UIFont systemFontOfSize:THfloat(16)];
  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. self.confirmBlock(self.selectedValue);
  76. [self dismissView];
  77. }
  78. - (void)showView{
  79. [[UIApplication sharedApplication].keyWindow addSubview:self];
  80. self.alpha = 0;
  81. [UIView animateWithDuration:0.3 animations:^{
  82. self.alpha = 1;
  83. } completion:^(BOOL finished) {
  84. }];
  85. }
  86. - (void)dismissView{
  87. [UIView animateWithDuration:0.3 animations:^{
  88. self.alpha = 0;
  89. } completion:^(BOOL finished) {
  90. [self removeFromSuperview];
  91. }];
  92. }
  93. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  94. [self dismissView];
  95. }
  96. #pragma mark - pickerView 代理方法
  97. // 列数
  98. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  99. return 1;
  100. }
  101. // 行数
  102. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  103. return self.questionArray.count;
  104. }
  105. -(CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component {
  106. return THfloat(35);
  107. }
  108. // 显示什么
  109. - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  110. return self.questionArray[row];
  111. }
  112. // 选中时
  113. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  114. self.selectedValue = row;
  115. }
  116. - (UIPickerView *)pickerView {
  117. if (!_pickerView) {
  118. _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, THfloat(45), THScreenW, THfloat(180))];
  119. _pickerView.backgroundColor = [UIColor clearColor];
  120. _pickerView.dataSource = self;
  121. _pickerView.delegate = self;
  122. // _pickerView 初始化显示的值
  123. if (self.defaultDesc.length == 0) {
  124. //无默认值时显示第一个
  125. [_pickerView selectRow:0 inComponent:0 animated:YES];
  126. }else {
  127. //有默认值时显示默认值
  128. recordRowOfQuestion = [self rowOfQuestionWithName:self.defaultDesc];
  129. [_pickerView selectRow:recordRowOfQuestion inComponent:0 animated:YES];
  130. self.selectedValue = recordRowOfQuestion;
  131. }
  132. }
  133. return _pickerView;
  134. }
  135. - (NSInteger)rowOfQuestionWithName:(NSString *)questionName{
  136. NSInteger row = 0;
  137. for (NSString *str in self.questionArray) {
  138. if ([str containsString:questionName]) {
  139. NSLog(@"row = %ld",row);
  140. return row;
  141. }
  142. row++;
  143. }
  144. return row;
  145. }
  146. @end