YOUPAIHRSelectDayDateVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // YOUPAIHRSelectDayDateVC.m
  3. // VQU
  4. //
  5. // Created by xiaohaoran on 2021/11/5.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAIHRSelectDayDateVC.h"
  9. #import "BRPickerView.h"
  10. typedef NS_ENUM(NSInteger, BRTimeType) {
  11. BRTimeTypeBeginTime = 0,
  12. BRTimeTypeEndTime
  13. };
  14. @interface YOUPAIHRSelectDayDateVC ()<UITextFieldDelegate>
  15. @property (nonatomic, strong) BRDatePickerView *youpaipdatePickerView;
  16. @property (nonatomic, strong) NSDate *youpaipbeginSelectDate;
  17. @property (nonatomic, strong) NSDate *youpaipendSelectDate;
  18. @property (nonatomic, strong) UITextField *youpaipbeginTimeTF;
  19. @property (nonatomic, strong) UITextField *youpaipendTimeTF;
  20. @property (nonatomic, assign) BRTimeType youpaiptimeType;
  21. @end
  22. @implementation YOUPAIHRSelectDayDateVC
  23. -(void)viewDidLoad{
  24. [super viewDidLoad];
  25. [self youpaifsetupUI];
  26. }
  27. #pragma mark - UITextFieldDelegate
  28. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  29. [self.view endEditing:YES];
  30. [self youpaifhandlerTextFieldSelect:textField];
  31. return NO; // 当前 textField 不可编辑,可以响应点击事件
  32. }
  33. - (void)youpaifhandlerTextFieldSelect:(UITextField *)textField {
  34. if (textField.tag == 100) {
  35. self.youpaiptimeType = BRTimeTypeBeginTime;
  36. self.youpaipendTimeTF.textColor = HexColorFromRGBA(0xFFFFFF,0.2);
  37. self.youpaipbeginTimeTF.textColor = [UIColor whiteColor];
  38. NSString *format = @"yyyy-MM-dd";
  39. if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYM) {
  40. format = @"yyyy-MM";
  41. } else if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYMDH) {
  42. format = @"yyyy-MM-dd HH";
  43. }
  44. // 设置选择器滚动到指定的日期
  45. //self.youpaipdatePickerView.selectValue = self.youpaipbeginTimeTF.text;
  46. self.youpaipdatePickerView.selectDate = self.youpaipbeginSelectDate;
  47. }else{
  48. self.youpaiptimeType = BRTimeTypeEndTime;
  49. self.youpaipbeginTimeTF.textColor = HexColorFromRGBA(0xFFFFFF,0.2);
  50. self.youpaipendTimeTF.textColor = [UIColor whiteColor];
  51. NSString *format = @"yyyy-MM-dd";
  52. if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYM) {
  53. format = @"yyyy-MM";
  54. } else if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYMDH) {
  55. format = @"yyyy-MM-dd HH";
  56. }
  57. // 设置选择器滚动到指定的日期
  58. //self.youpaipdatePickerView.selectValue = self.youpaipendTimeTF.text;
  59. self.youpaipdatePickerView.selectDate = self.youpaipendSelectDate;
  60. }
  61. }
  62. -(void)youpaifsetupUI{
  63. UIView *bottomView = [UIView new];
  64. [self.view addSubview:bottomView];
  65. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.right.bottom.offset(0.0f);
  67. make.height.offset(324+SafeHeight-49);
  68. }];
  69. bottomView.backgroundColor = LCBkgColor;
  70. //youpaipheaderView
  71. UIView *youpaipheaderView = [UIView new];
  72. [bottomView addSubview:youpaipheaderView];
  73. [youpaipheaderView mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.left.mas_equalTo(0);
  75. make.right.mas_equalTo(0);
  76. make.top.mas_equalTo(0);
  77. make.height.mas_equalTo(60);
  78. }];
  79. youpaipheaderView.backgroundColor = HexColorFromRGBA(0x4F4B5B, 0.2);
  80. //leftView
  81. UIView *leftBgView = [UIView new];
  82. [youpaipheaderView addSubview:leftBgView];
  83. [leftBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.left.mas_equalTo(0);
  85. make.width.mas_equalTo(KScreenWidth/2);
  86. make.top.mas_equalTo(0);
  87. make.bottom.mas_equalTo(0);
  88. }];
  89. leftBgView.backgroundColor = [UIColor clearColor];
  90. //title
  91. UILabel *beginTimeLabel = [UILabel new];
  92. [leftBgView addSubview:beginTimeLabel];
  93. [beginTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.centerX.mas_equalTo(0);
  95. make.top.mas_equalTo(14);
  96. make.width.mas_equalTo(120);
  97. }];
  98. beginTimeLabel.text = @"开始时间";
  99. beginTimeLabel.font = [UIFont systemFontOfSize:14];
  100. beginTimeLabel.textColor = [UIColor whiteColor];
  101. beginTimeLabel.textAlignment = NSTextAlignmentCenter;
  102. //时间
  103. UITextField *youpaipbeginTimeTF = [UITextField new];
  104. self.youpaipbeginTimeTF = youpaipbeginTimeTF;
  105. [leftBgView addSubview:youpaipbeginTimeTF];
  106. [youpaipbeginTimeTF mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.centerX.mas_equalTo(0);
  108. make.top.mas_equalTo(beginTimeLabel.mas_bottom).offset(6);
  109. make.width.mas_equalTo(120);
  110. }];
  111. youpaipbeginTimeTF.delegate = self;
  112. youpaipbeginTimeTF.tag = 100;
  113. youpaipbeginTimeTF.textAlignment = NSTextAlignmentCenter;
  114. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"选择时间" attributes:
  115. @{NSForegroundColorAttributeName:[UIColor whiteColor],
  116. NSFontAttributeName:youpaipbeginTimeTF.font
  117. }];
  118. youpaipbeginTimeTF.attributedPlaceholder = attrString;
  119. youpaipbeginTimeTF.textColor = [UIColor whiteColor];
  120. youpaipbeginTimeTF.font = [UIFont systemFontOfSize:11];
  121. youpaipbeginTimeTF.textAlignment = NSTextAlignmentCenter;
  122. //leftView
  123. UIView *rightBgView = [UIView new];
  124. [youpaipheaderView addSubview:rightBgView];
  125. [rightBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  126. make.right.mas_equalTo(0);
  127. make.width.mas_equalTo(KScreenWidth/2);
  128. make.top.mas_equalTo(0);
  129. make.bottom.mas_equalTo(0);
  130. }];
  131. rightBgView.backgroundColor = [UIColor clearColor];
  132. //title
  133. UILabel *endTimeLabel = [UILabel new];
  134. [rightBgView addSubview:endTimeLabel];
  135. [endTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.centerX.mas_equalTo(0);
  137. make.top.mas_equalTo(14);
  138. make.width.mas_equalTo(120);
  139. }];
  140. endTimeLabel.text = @"结束时间";
  141. endTimeLabel.font = [UIFont systemFontOfSize:14];
  142. endTimeLabel.textColor = [UIColor whiteColor];
  143. endTimeLabel.textAlignment = NSTextAlignmentCenter;
  144. //时间
  145. UITextField *youpaipendTimeTF = [UITextField new];
  146. self.youpaipendTimeTF = youpaipendTimeTF;
  147. [rightBgView addSubview:youpaipendTimeTF];
  148. [youpaipendTimeTF mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.centerX.mas_equalTo(0);
  150. make.top.mas_equalTo(endTimeLabel.mas_bottom).offset(6);
  151. make.width.mas_equalTo(120);
  152. }];
  153. youpaipendTimeTF.delegate = self;
  154. youpaipendTimeTF.tag = 101;
  155. youpaipendTimeTF.textAlignment = NSTextAlignmentCenter;
  156. NSAttributedString *attrString1 = [[NSAttributedString alloc] initWithString:@"选择时间" attributes:
  157. @{NSForegroundColorAttributeName:[UIColor whiteColor],
  158. NSFontAttributeName:youpaipendTimeTF.font
  159. }];
  160. youpaipendTimeTF.attributedPlaceholder = attrString1;
  161. youpaipendTimeTF.textColor = [UIColor whiteColor];
  162. youpaipendTimeTF.font = [UIFont systemFontOfSize:11];
  163. youpaipendTimeTF.textAlignment = NSTextAlignmentCenter;
  164. // 创建选择器容器视图
  165. UIView *containerView = [[UIView alloc]init];
  166. [bottomView addSubview:containerView];
  167. [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  168. make.top.mas_equalTo(youpaipheaderView.mas_bottom);
  169. make.left.mas_equalTo(0);
  170. make.right.mas_equalTo(0);
  171. make.bottom.mas_equalTo(-SafeHeight);
  172. }];
  173. containerView.backgroundColor = LCBkgColor;
  174. // 4.创建日期选择器
  175. BRDatePickerView *youpaipdatePickerView = [[BRDatePickerView alloc]init];
  176. youpaipdatePickerView.pickerMode = BRDatePickerModeYMD;
  177. youpaipdatePickerView.maxDate = [NSDate date];
  178. youpaipdatePickerView.isAutoSelect = YES;
  179. youpaipdatePickerView.showUnitType = BRShowUnitTypeOnlyCenter;
  180. youpaipdatePickerView.resultBlock = ^(NSDate *youpaipselectDate, NSString *selectValue) {
  181. if (self.youpaiptimeType == BRTimeTypeBeginTime) {
  182. self.youpaipbeginSelectDate = youpaipselectDate;
  183. self.youpaipbeginTimeTF.text = selectValue;
  184. } else if (self.youpaiptimeType == BRTimeTypeEndTime) {
  185. self.youpaipendSelectDate = youpaipselectDate;
  186. self.youpaipendTimeTF.text = selectValue;
  187. }
  188. };
  189. containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  190. // 自定义选择器主题样式
  191. BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
  192. customStyle.pickerColor = [UIColor clearColor];
  193. youpaipdatePickerView.pickerStyle = customStyle;
  194. self.youpaipdatePickerView = youpaipdatePickerView;
  195. customStyle.pickerTextColor = HexColorFromRGBA(0xFFFFFF, 0.2);
  196. customStyle.separatorColor = [UIColor clearColor];
  197. customStyle.selectRowTextColor = [UIColor whiteColor];
  198. customStyle.dateUnitTextColor = [UIColor whiteColor];
  199. // 添加选择器到容器视图
  200. [youpaipdatePickerView addPickerToView:containerView];
  201. //确定按钮
  202. UIButton *submitBtn = [UIButton new];
  203. [self.view addSubview:submitBtn];
  204. [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  205. make.centerX.mas_equalTo(0);
  206. make.bottom.mas_equalTo(-SafeHeight);
  207. make.size.mas_equalTo(CGSizeMake(294, 49));
  208. }];
  209. [submitBtn setBackgroundImage:[LCTools ColorImage:CGSizeMake(294, 49) FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:1] forState:UIControlStateNormal];
  210. [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
  211. [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  212. submitBtn.titleLabel.font = LCBoldFont(16);
  213. [submitBtn addTarget:self action:@selector(youpaifsubmitBtnClick) forControlEvents:UIControlEventTouchUpInside];
  214. submitBtn.layer.cornerRadius = 49/2;
  215. submitBtn.layer.masksToBounds = YES;
  216. }
  217. -(void)youpaifsubmitBtnClick{
  218. if (self.youpaipbeginSelectDate == nil) {
  219. [ZCHUDHelper showTitle:@"请选择开始时间"];
  220. return;
  221. }
  222. if (self.youpaipendSelectDate == nil) {
  223. [ZCHUDHelper showTitle:@"请选择结束时间"];
  224. return;
  225. }
  226. if ([self youpaifcompareDate:self.youpaipbeginTimeTF.text withDate:self.youpaipendTimeTF.text] == 1) {
  227. }else{
  228. [ZCHUDHelper showTitle:@"结束时间要大于开始时间"];
  229. }
  230. if (self.youpaipdidSelectTimeBlock) {
  231. self.youpaipdidSelectTimeBlock(self.youpaipbeginTimeTF.text, self.youpaipendTimeTF.text);
  232. }
  233. }
  234. /**
  235. 比较两个日期的大小
  236. 日期格式为:yyyy-MM-dd HH-mm
  237. */
  238. -(int)youpaifcompareDate:(NSString*)date01 withDate:(NSString*)date02{
  239. int ci;
  240. NSDateFormatter *df = [[NSDateFormatter alloc] init];
  241. [df setDateFormat:@"yyyy-MM-dd"];
  242. NSDate *dt1 = [[NSDate alloc] init];
  243. NSDate *dt2 = [[NSDate alloc] init];
  244. dt1 = [df dateFromString:date01];
  245. dt2 = [df dateFromString:date02];
  246. NSComparisonResult result = [dt1 compare:dt2];
  247. switch (result)
  248. {
  249. //date02比date01大
  250. case NSOrderedAscending: ci=1; break;
  251. //date02比date01小
  252. case NSOrderedDescending: ci=-1; break;
  253. //date02=date01
  254. // case NSOrderedSame: ci=0; break;
  255. default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
  256. }
  257. return ci;
  258. }
  259. #pragma mark - JXCategoryListContentViewDelegate
  260. - (UIView *)listView{
  261. return self.view;
  262. }
  263. @end