PickerChoiceView.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // PickerChoiceView.m
  3. // TFPickerView
  4. //
  5. // Created by TF_man on 16/5/11.
  6. // Copyright © 2016年 tituanwang. All rights reserved.
  7. //
  8. //屏幕宽和高
  9. #define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
  10. #define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
  11. //RGB
  12. #define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
  13. // 缩放比
  14. #define kScale ([UIScreen mainScreen].bounds.size.width) / 375
  15. #define hScale ([UIScreen mainScreen].bounds.size.height) / 667
  16. //字体大小
  17. #define kfont 15
  18. #import "PickerChoiceView.h"
  19. //#import "Masonry.h"
  20. @interface PickerChoiceView ()<UIPickerViewDelegate,UIPickerViewDataSource>
  21. @property (nonatomic,strong)UIView *bgV;
  22. @property (nonatomic,strong)UIButton *cancelBtn;
  23. @property (nonatomic,strong)UIButton *conpleteBtn;
  24. @property (nonatomic,strong)UIPickerView *pickerV;
  25. @property (nonatomic,strong)NSMutableArray *array;
  26. @end
  27. @implementation PickerChoiceView
  28. - (instancetype)initWithFrame:(CGRect)frame{
  29. if (self = [super initWithFrame:frame]) {
  30. self.array = [NSMutableArray array];
  31. self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
  32. self.backgroundColor = RGBA(51, 51, 51, 0.8);
  33. self.bgV = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260*hScale)];
  34. self.bgV.backgroundColor = [UIColor whiteColor];
  35. [self addSubview:self.bgV];
  36. [self showAnimation];
  37. //取消
  38. self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  39. [self.bgV addSubview:self.cancelBtn];
  40. self.cancelBtn.sd_layout
  41. .topSpaceToView(self.bgV,0)
  42. .leftSpaceToView(self.bgV,15)
  43. .widthIs(40)
  44. .heightIs(44);
  45. self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:kfont];
  46. [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  47. [self.cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  48. [self.cancelBtn setTitleColor:RGBA(0, 122, 255, 1) forState:UIControlStateNormal];
  49. //完成
  50. self.conpleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  51. [self.bgV addSubview:self.conpleteBtn];
  52. self.conpleteBtn.sd_layout
  53. .topSpaceToView(self.bgV,0)
  54. .rightSpaceToView(self.bgV,15)
  55. .widthIs(40)
  56. .heightIs(44);
  57. self.conpleteBtn.titleLabel.font = [UIFont systemFontOfSize:kfont];
  58. [self.conpleteBtn setTitle:@"完成" forState:UIControlStateNormal];
  59. [self.conpleteBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
  60. [self.conpleteBtn setTitleColor:RGBA(0, 122, 255, 1) forState:UIControlStateNormal];
  61. //选择titi
  62. self.selectLb = [UILabel new];
  63. [self.selectLb setSingleLineAutoResizeWithMaxWidth:0];
  64. [self.bgV addSubview:self.selectLb];
  65. self.selectLb.sd_layout
  66. .centerXEqualToView(self.bgV)
  67. .centerYEqualToView(self.conpleteBtn)
  68. .heightIs(44);
  69. self.selectLb.font = [UIFont systemFontOfSize:kfont];
  70. self.selectLb.textAlignment = NSTextAlignmentCenter;
  71. //线
  72. UIView *line = [UIView new];
  73. [self.bgV addSubview:line];
  74. line.sd_layout
  75. .leftEqualToView(self.bgV)
  76. .topSpaceToView(self.conpleteBtn,0)
  77. .rightEqualToView(self.bgV)
  78. .widthIs(SCREEN_WIDTH)
  79. .heightIs(0.5);
  80. line.backgroundColor = RGBA(224, 224, 224, 1);
  81. //选择器
  82. self.pickerV = [UIPickerView new];
  83. [self.bgV addSubview:self.pickerV];
  84. self.pickerV.sd_layout
  85. .topSpaceToView(line,0)
  86. .bottomEqualToView(self.bgV)
  87. .leftEqualToView(self.bgV)
  88. .rightEqualToView(self.bgV);
  89. self.pickerV.delegate = self;
  90. self.pickerV.dataSource = self;
  91. }
  92. return self;
  93. }
  94. - (void)setCustomArr:(NSArray *)customArr{
  95. _customArr = customArr;
  96. [self.array addObject:customArr];
  97. }
  98. - (void)setArrayType:(ARRAYTYPE)arrayType
  99. {
  100. _arrayType = arrayType;
  101. switch (arrayType) {
  102. case GenderArray:
  103. {
  104. self.selectLb.text = @"选择性别";
  105. [self.array addObject:@[@"男",@"女"]];
  106. }
  107. break;
  108. case HeightArray:
  109. {
  110. self.selectLb.text = @"选择身高";
  111. NSMutableArray *arr = [NSMutableArray array];
  112. for (int i = 100; i <= 250; i++) {
  113. NSString *str = [NSString stringWithFormat:@"%d",i];
  114. [arr addObject:str];
  115. }
  116. [self.array addObject:(NSArray *)arr];
  117. }
  118. break;
  119. case weightArray:
  120. {
  121. self.selectLb.text = @"选择体重";
  122. NSMutableArray *arr = [NSMutableArray array];
  123. for (int i = 30; i <= 200; i++) {
  124. NSString *str = [NSString stringWithFormat:@"%d",i];
  125. [arr addObject:str];
  126. }
  127. [self.array addObject:(NSArray *)arr];
  128. }
  129. break;
  130. case DeteArray:
  131. {
  132. self.selectLb.text = @"选择出生年月";
  133. [self creatDate];
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. - (void)creatDate{
  141. NSMutableArray *yearArray = [[NSMutableArray alloc] init];
  142. for (int i = 1970; i <= 2050 ; i++)
  143. {
  144. [yearArray addObject:[NSString stringWithFormat:@"%d年",i]];
  145. }
  146. [self.array addObject:yearArray];
  147. NSMutableArray *monthArray = [[NSMutableArray alloc]init];
  148. for (int i = 1; i < 13; i ++) {
  149. [monthArray addObject:[NSString stringWithFormat:@"%d月",i]];
  150. }
  151. [self.array addObject:monthArray];
  152. NSMutableArray *daysArray = [[NSMutableArray alloc]init];
  153. for (int i = 1; i < 32; i ++) {
  154. [daysArray addObject:[NSString stringWithFormat:@"%d日",i]];
  155. }
  156. [self.array addObject:daysArray];
  157. NSDate *date = [NSDate date];
  158. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  159. [formatter setDateFormat:@"yyyy"];
  160. NSString *currentYear = [NSString stringWithFormat:@"%@年",[formatter stringFromDate:date]];
  161. [self.pickerV selectRow:[yearArray indexOfObject:currentYear]+81*50 inComponent:0 animated:YES];
  162. [formatter setDateFormat:@"MM"];
  163. NSString *currentMonth = [NSString stringWithFormat:@"%ld月",(long)[[formatter stringFromDate:date]integerValue]];
  164. [self.pickerV selectRow:[monthArray indexOfObject:currentMonth]+12*50 inComponent:1 animated:YES];
  165. [formatter setDateFormat:@"dd"];
  166. NSString *currentDay = [NSString stringWithFormat:@"%@日",[formatter stringFromDate:date]];
  167. [self.pickerV selectRow:[daysArray indexOfObject:currentDay]+31*50 inComponent:2 animated:YES];
  168. }
  169. #pragma mark-----UIPickerViewDataSource
  170. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  171. return self.array.count;
  172. }
  173. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  174. NSArray * arr = (NSArray *)[self.array objectAtIndex:component];
  175. if (self.arrayType == DeteArray) {
  176. return arr.count*100;
  177. }else{
  178. return arr.count;
  179. }
  180. }
  181. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  182. UILabel *label=[[UILabel alloc] init];
  183. label.textAlignment = NSTextAlignmentCenter;
  184. label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
  185. return label;
  186. }
  187. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  188. NSArray *arr = (NSArray *)[self.array objectAtIndex:component];
  189. return [arr objectAtIndex:row % arr.count];
  190. }
  191. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
  192. {
  193. if (self.arrayType == DeteArray) {
  194. return 60;
  195. }else{
  196. return 110;
  197. }
  198. }
  199. #pragma mark-----点击方法
  200. - (void)cancelBtnClick{
  201. [self hideAnimation];
  202. }
  203. - (void)completeBtnClick{
  204. NSString *fullStr = [NSString string];
  205. for (int i = 0; i < self.array.count; i++) {
  206. NSArray *arr = [self.array objectAtIndex:i];
  207. if (self.arrayType == DeteArray) {
  208. NSString *str = [arr objectAtIndex:[self.pickerV selectedRowInComponent:i]% arr.count];
  209. fullStr = [fullStr stringByAppendingString:str];
  210. }else{
  211. NSString *str = [arr objectAtIndex:[self.pickerV selectedRowInComponent:i]];
  212. fullStr = [fullStr stringByAppendingString:str];
  213. }
  214. }
  215. [self.delegate PickerSelectorIndixString:fullStr];
  216. [self hideAnimation];
  217. }
  218. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  219. [self hideAnimation];
  220. }
  221. //隐藏动画
  222. - (void)hideAnimation{
  223. [UIView animateWithDuration:0.5 animations:^{
  224. CGRect frame = self.bgV.frame;
  225. frame.origin.y = kScreenHeight;
  226. self.bgV.frame = frame;
  227. } completion:^(BOOL finished) {
  228. [self.bgV removeFromSuperview];
  229. [self removeFromSuperview];
  230. }];
  231. }
  232. //显示动画
  233. - (void)showAnimation{
  234. [UIView animateWithDuration:0.5 animations:^{
  235. CGRect frame = self.bgV.frame;
  236. frame.origin.y = kScreenHeight-260*hScale;
  237. self.bgV.frame = frame;
  238. }];
  239. }
  240. @end