WXPPickerView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // PickerView.m
  3. // Hu8huWorker
  4. //
  5. // Created by xiaopeng on 2017/4/24.
  6. // Copyright © 2017年 王小朋. All rights reserved.
  7. //
  8. #import "WXPPickerView.h"
  9. #define onePickerH 50
  10. #define midPickerH 50
  11. #define Screen_Width [[UIScreen mainScreen] bounds].size.width
  12. #define Screen_Height [[UIScreen mainScreen] bounds].size.height
  13. #define LTColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
  14. #define UIColorFromRGB(argbValue) [UIColor colorWithRed:((float)((argbValue & 0x00FF0000) >> 16))/255.0 green:((float)((argbValue & 0x0000FF00) >> 8))/255.0 blue:((float)(argbValue & 0x000000FF))/255.0 alpha:((float)((argbValue & 0xFF000000) >> 24))/255.0]
  15. @interface WXPPickerView ()<UIScrollViewDelegate>{
  16. UIWindow *_window;
  17. UITapGestureRecognizer *_gesture;
  18. UIView *_view;
  19. int _seleNum;
  20. }
  21. @property (strong, nonatomic) UIView * select;
  22. @property (strong, nonatomic) UIScrollView * startTime;
  23. @property (strong, nonatomic) UIView * all;
  24. @property (strong, nonatomic) UILabel * allText;
  25. @property (strong, nonatomic) UISwitch * allSwithch;
  26. @property (assign, nonatomic) NSInteger num;
  27. @property (strong, nonatomic) NSMutableArray * midArry;
  28. @end
  29. @implementation WXPPickerView
  30. - (instancetype)initWithFrame:(CGRect)frame midArry:(NSMutableArray *)midArry{
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. _midArry = midArry;
  34. /**
  35. 处理数组
  36. */
  37. [_midArry insertObject:@"数组第一位" atIndex:0];
  38. [_midArry insertObject:@" " atIndex:1];
  39. [_midArry addObject:@" "];
  40. [_midArry addObject:@"数组最后一位"];
  41. }
  42. return self;
  43. }
  44. - (void)addStart:(CGRect)frame leftArry:(NSArray *)leftArry{
  45. for(NSInteger index = 0; index < leftArry.count; index++){
  46. NSString * title = @"";
  47. if(index != 0 && index != leftArry.count-1){
  48. title = [NSString stringWithFormat:@"%@",leftArry[index]];
  49. }
  50. UILabel *_startTime1 = [[UILabel alloc] initWithFrame:CGRectMake(0, index*onePickerH, frame.size.width,onePickerH)];
  51. [_startTime1 setText:title];
  52. _startTime1.tag = 10 + index;
  53. [_startTime1 setTextAlignment:NSTextAlignmentCenter];
  54. if (index == 2) {
  55. [_startTime1 setTextColor:UIColorFromRGB(0xff38acff)];
  56. [_startTime1 setFont:[UIFont systemFontOfSize:15]];
  57. }else{
  58. [_startTime1 setTextColor:UIColorFromRGB(0xff333333)];
  59. [_startTime1 setFont:[UIFont systemFontOfSize:13]];
  60. }
  61. [self.startTime addSubview:_startTime1];
  62. }
  63. [self.startTime setContentSize:CGSizeMake(frame.size.width/2,onePickerH*leftArry.count)];
  64. }
  65. //4、已经结束拖拽,手指刚离开view的那一刻
  66. -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  67. if(!decelerate){
  68. int pointY=scrollView.contentOffset.y;
  69. CGFloat f = pointY %onePickerH;
  70. int s = pointY/onePickerH;
  71. int s1 = s;
  72. if(f>20){
  73. s1 = s+1;
  74. NSLog(@"%d",s1*onePickerH);
  75. [scrollView setContentOffset:CGPointMake(0, s1*onePickerH) animated:YES];
  76. }else{
  77. s1 = s;
  78. NSLog(@"%d",s1*onePickerH);
  79. [scrollView setContentOffset:CGPointMake(0, s1*onePickerH) animated:YES];
  80. }
  81. for (int i = 0; i < _num; i++) {
  82. if ( i == s1+2) {
  83. UILabel * textLabel = [self.startTime viewWithTag:i+10];
  84. NSLog(@"%@",textLabel.text);
  85. textLabel.textColor = UIColorFromRGB(0xff38acff);
  86. textLabel.font = [UIFont systemFontOfSize:15];
  87. }else{
  88. UILabel * textLabel = [self.startTime viewWithTag:i+10];
  89. textLabel.textColor = UIColorFromRGB(0xff333333);
  90. textLabel.font = [UIFont systemFontOfSize:13];
  91. }
  92. }
  93. _seleNum = s1;
  94. if ([self.delegate respondsToSelector:NSSelectorFromString(@"PickerViewOneDelegateOncleck:")]) {
  95. [self.delegate PickerViewOneDelegateOncleck:s1];
  96. }
  97. }
  98. }
  99. //6、view已经停止滚动
  100. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  101. int pointY=scrollView.contentOffset.y;
  102. CGFloat f = pointY %onePickerH;
  103. int s = pointY/onePickerH;
  104. int s1;
  105. if(f>20){
  106. s1 = s+1;
  107. NSLog(@"%d",s1*onePickerH);
  108. [scrollView setContentOffset:CGPointMake(0, s1*onePickerH) animated:YES];
  109. }else{
  110. s1 = s;
  111. NSLog(@"%d",s1*onePickerH);
  112. [scrollView setContentOffset:CGPointMake(0, s1*onePickerH) animated:YES];
  113. }
  114. for (int i = 0; i < _num; i++) {
  115. if ( i == s1+2) {
  116. UILabel * textLabel = [self.startTime viewWithTag:i+10];
  117. NSLog(@"%@",textLabel.text);
  118. textLabel.textColor = UIColorFromRGB(0xff38acff);
  119. textLabel.font = [UIFont systemFontOfSize:15];
  120. }else{
  121. UILabel * textLabel = [self.startTime viewWithTag:i+10];
  122. textLabel.textColor = UIColorFromRGB(0xff333333);
  123. textLabel.font = [UIFont systemFontOfSize:13];
  124. }
  125. }
  126. _seleNum = s1;
  127. if ([self.delegate respondsToSelector:NSSelectorFromString(@"PickerViewOneDelegateOncleck:")]) {
  128. [self.delegate PickerViewOneDelegateOncleck:s1];
  129. }
  130. }
  131. #pragma mark 打开与关闭方法
  132. -(void)show{
  133. [self setUI];
  134. [UIView animateWithDuration:0.3 animations:^{
  135. self.frame = CGRectMake(0, Screen_Height - 300, Screen_Height, 300);
  136. }];
  137. }
  138. -(void)close{
  139. //移除点击手势
  140. [_window removeGestureRecognizer:_gesture];
  141. _gesture = nil;
  142. [UIView animateWithDuration:0.2 animations:^{
  143. self.frame = CGRectMake(0, Screen_Height, Screen_Width, 300);
  144. } completion:^(BOOL finished) {
  145. for(id subv in [self subviews])
  146. {
  147. [subv removeFromSuperview];
  148. }
  149. [_view removeFromSuperview];
  150. }];
  151. }
  152. - (void)setUI{
  153. self.frame = CGRectMake(0, Screen_Height, Screen_Width, 300);
  154. _num = _midArry.count;
  155. UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
  156. [topView setBackgroundColor:UIColorFromRGB(0xff38acff)];
  157. [self addSubview:topView];
  158. UIButton *leftBtn = [[UIButton alloc]init];
  159. leftBtn.frame = CGRectMake(15, 0, 40, 50);
  160. [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
  161. [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  162. [leftBtn addTarget:self action:@selector(leftbtnOnclick) forControlEvents:UIControlEventTouchUpInside];
  163. [topView addSubview:leftBtn];
  164. if ([_isTitle isEqualToString:@"1"]) {
  165. UILabel *titleLable = [[UILabel alloc]init];
  166. titleLable.frame = CGRectMake(Screen_Width/2-50, 0, 100, 50);
  167. titleLable.text = _title;
  168. titleLable.textAlignment = NSTextAlignmentCenter;
  169. titleLable.font = [UIFont systemFontOfSize:18];
  170. titleLable.textColor = [UIColor whiteColor];
  171. [topView addSubview:titleLable];
  172. }
  173. UIButton *rightBtn = [[UIButton alloc]init];
  174. rightBtn.frame = CGRectMake(Screen_Width-15-100, 0, 100, 50);
  175. [rightBtn setTitle:_rightBtnTitle forState:UIControlStateNormal];
  176. rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  177. [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  178. [rightBtn addTarget:self action:@selector(rightBtnOnclick) forControlEvents:UIControlEventTouchUpInside];
  179. [topView addSubview:rightBtn];
  180. self.select = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 250)];
  181. [self addSubview:self.select];
  182. self.startTime = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-50)];
  183. self.startTime.delegate = self;
  184. [self.startTime setShowsVerticalScrollIndicator:NO];
  185. [self.startTime setShowsHorizontalScrollIndicator:NO];
  186. [self.select addSubview:self.startTime];
  187. [self addStart:self.frame leftArry:_midArry];
  188. UIView * seleViewColor = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, 50)];
  189. [seleViewColor setBackgroundColor:UIColorFromRGB(0xfff3faff)];
  190. [self.select addSubview:seleViewColor];
  191. [self.select bringSubviewToFront:self.startTime];
  192. UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, onePickerH, self.frame.size.width, 1)];
  193. [line1 setBackgroundColor:UIColorFromRGB(0xffc3e1f6)];
  194. [self addSubview:line1];
  195. // UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(0, onePickerH*2, self.frame.size.width, 1)];
  196. // [line2 setBackgroundColor:UIColorFromRGB(0xffc3e1f6)];
  197. // [self addSubview:line2];
  198. UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, onePickerH*3, self.frame.size.width, 1)];
  199. [line3 setBackgroundColor:UIColorFromRGB(0xffc3e1f6)];
  200. [self addSubview:line3];
  201. UIView * line4 = [[UIView alloc] initWithFrame:CGRectMake(0, onePickerH*4, self.frame.size.width, 1)];
  202. [line4 setBackgroundColor:UIColorFromRGB(0xffc3e1f6)];
  203. [self addSubview:line4];
  204. // UIView * line5 = [[UIView alloc] initWithFrame:CGRectMake(0, onePickerH*5, self.frame.size.width, 1)];
  205. // [line5 setBackgroundColor:UIColorFromRGB(0xffc3e1f6)];
  206. // [self addSubview:line5];
  207. _window = [UIApplication sharedApplication].keyWindow;
  208. [_window addSubview:self];
  209. _gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close)];
  210. [_window addGestureRecognizer:_gesture];
  211. _view = [[UIView alloc]initWithFrame:_window.bounds];
  212. _view.backgroundColor = LTColor(0, 0, 0, 0.8);
  213. [_window addSubview:_view];
  214. [_view addSubview:self];
  215. }
  216. - (void)leftbtnOnclick{
  217. [self close];
  218. }
  219. - (void)rightBtnOnclick{
  220. if ([self.delegate respondsToSelector:NSSelectorFromString(@"PickerViewRightButtonOncleck:")]) {
  221. [self.delegate PickerViewRightButtonOncleck:_seleNum];
  222. }
  223. }
  224. @end