DateView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #import "DateView.h"
  2. @implementation DateView
  3. -(instancetype)init
  4. {
  5. self = [super initWithFrame:kFrame];
  6. if (self) {
  7. [self setBackgroundColor:[UIColor colorWithWhite:.01 alpha:.3]];
  8. CGFloat x,y,w,h,bd;
  9. x = bd = 10;
  10. y = 0;
  11. w = kSize.width - bd*2;
  12. h = 300;
  13. UIView* aView;
  14. aView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  15. [aView setBackgroundColor:backGroundColor];
  16. aView.center = CGPointMake(kSize.width/2.0, kSize.height/2.0);
  17. [self addSubview:aView];
  18. UILabel* label;
  19. x = bd;
  20. h = 40;
  21. w = aView.width - bd*2;
  22. label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w , h)];
  23. [label setText:@"请选择时间"];
  24. [label setTextAlignment:NSTextAlignmentCenter];
  25. [aView addSubview:label];
  26. UIView* line;
  27. y += h;
  28. h = 3;
  29. x = bd;
  30. line = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  31. [aView addSubview:line];
  32. [line setBackgroundColor:RQ_MAIN_COLOR];
  33. y += h;
  34. h = 200;
  35. picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(x, y, w , h)];
  36. [picker setDatePickerMode:UIDatePickerModeDate];
  37. if (@available(iOS 13.4, *)) {
  38. picker.preferredDatePickerStyle = UIDatePickerStyleWheels;
  39. } else {
  40. // Fallback on earlier versions
  41. }
  42. [aView addSubview:picker];
  43. x = 60;
  44. y += h + bd;
  45. w = (kSize.width- bd*2 - 60*3)/2.0;
  46. h = aView.height - y - bd;
  47. UIButton* btn;
  48. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  49. [btn setTitle:@"取消" forState:UIControlStateNormal];
  50. [btn setBackgroundColor:RQ_MAIN_COLOR];
  51. [btn addTarget:self action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  52. [aView addSubview:btn];
  53. btn.layer.cornerRadius = btn.height*.5;
  54. btn.layer.masksToBounds = YES;
  55. x += w+60;
  56. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  57. [btn setTitle:@"确定" forState:UIControlStateNormal];
  58. [btn addTarget:self action:@selector(yesClick) forControlEvents:UIControlEventTouchUpInside];
  59. [btn setBackgroundColor:RQ_MAIN_COLOR];
  60. [aView addSubview:btn];
  61. btn.layer.cornerRadius = btn.height*.5;
  62. btn.layer.masksToBounds = YES;
  63. }
  64. return self;
  65. }
  66. -(void)setStyle:(int)style
  67. {
  68. _style = style;
  69. NSDateFormatter * formatter = [[ NSDateFormatter alloc ] init ];
  70. NSString * mindateStr ;
  71. NSString * maxdateStr;
  72. if (0 == _style || 2 == _style) {
  73. [picker setDatePickerMode:UIDatePickerModeDate];
  74. [formatter setDateFormat : @"yyyy"];
  75. int year = [formatter stringFromDate:[NSDate date]].intValue;
  76. [formatter setDateFormat : @"yyyy-MM-dd"];
  77. mindateStr = [NSString stringWithFormat:@"%d-01-01",year];
  78. maxdateStr = [NSString stringWithFormat:@"%d-12-31",year + 2];
  79. }
  80. if (1 == _style) {
  81. [picker setDatePickerMode:UIDatePickerModeTime];
  82. }
  83. if (3 == _style) {
  84. [picker setDatePickerMode:UIDatePickerModeTime];
  85. //这个是设置分钟轮上面时间间隔的
  86. picker.minuteInterval = 30;
  87. }
  88. NSDate * minDate = [formatter dateFromString :mindateStr];
  89. NSDate * maxDate = [formatter dateFromString :maxdateStr];
  90. if (maxDate) {
  91. picker.minimumDate = min;
  92. picker.maximumDate = max;
  93. }else{
  94. picker.minimumDate = minDate;
  95. picker.maximumDate = maxDate;
  96. }
  97. }
  98. -(void)setMaxDate:(NSDate *)maxDate MinDate:(NSDate *)minDate
  99. {
  100. max = maxDate;
  101. min = minDate;
  102. }
  103. -(void)yesClick
  104. {
  105. if (complete) {
  106. NSDateFormatter* fm = [NSDateFormatter new];
  107. fm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  108. if (0 == _style) {
  109. [fm setDateFormat:@"yyyy-MM-dd"];
  110. }else if (1 == _style || 3 == _style){
  111. [fm setDateFormat:@"HH:mm"];
  112. }else if (2 == _style) {
  113. [fm setDateFormat:@"EEE yyyy-MM-dd"];
  114. }
  115. complete([fm stringFromDate:picker.date]);
  116. [self removeFromSuperview];
  117. }
  118. }
  119. -(void)showWithComplete:(MyBlockType)comp
  120. {
  121. complete = comp;
  122. [[UIApplication sharedApplication].keyWindow addSubview:self];
  123. }
  124. @end