DateView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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:[UIColor whiteColor]];
  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:COLOR_THEME];
  33. y += h;
  34. h = 200;
  35. picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(x, y, w , h)];
  36. [picker setDatePickerMode:UIDatePickerModeDate];
  37. [aView addSubview:picker];
  38. x = 60;
  39. y += h + bd;
  40. w = (kSize.width- bd*2 - 60*3)/2.0;
  41. h = aView.height - y - bd;
  42. UIButton* btn;
  43. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  44. [btn setTitle:@"取消" forState:UIControlStateNormal];
  45. [btn setBackgroundColor:COLOR_THEME];
  46. [btn addTarget:self action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  47. [aView addSubview:btn];
  48. btn.layer.cornerRadius = btn.height*.5;
  49. btn.layer.masksToBounds = YES;
  50. x += w+60;
  51. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
  52. [btn setTitle:@"确定" forState:UIControlStateNormal];
  53. [btn addTarget:self action:@selector(yesClick) forControlEvents:UIControlEventTouchUpInside];
  54. [btn setBackgroundColor:COLOR_THEME];
  55. [aView addSubview:btn];
  56. btn.layer.cornerRadius = btn.height*.5;
  57. btn.layer.masksToBounds = YES;
  58. }
  59. return self;
  60. }
  61. -(void)setStyle:(int)style
  62. {
  63. _style = style;
  64. NSDateFormatter * formatter = [[ NSDateFormatter alloc ] init ];
  65. NSString * mindateStr = @"";
  66. NSString * maxdateStr = @"";
  67. if (0 == _style || 2 == _style) {
  68. [picker setDatePickerMode:UIDatePickerModeDate];
  69. [formatter setDateFormat : @"yyyy"];
  70. int year = [formatter stringFromDate:[NSDate date]].intValue;
  71. [formatter setDateFormat : @"yyyy-MM-dd"];
  72. mindateStr = [NSString stringWithFormat:@"%d-01-01",year];
  73. maxdateStr = [NSString stringWithFormat:@"%d-12-31",year + 2];
  74. }
  75. if (1 == _style) {
  76. [picker setDatePickerMode:UIDatePickerModeTime];
  77. }
  78. if (3 == _style) {
  79. [picker setDatePickerMode:UIDatePickerModeTime];
  80. //这个是设置分钟轮上面时间间隔的
  81. picker.minuteInterval = 30;
  82. }
  83. NSDate * minDate = [formatter dateFromString :mindateStr];
  84. NSDate * maxDate = [formatter dateFromString :maxdateStr];
  85. if (maxDate) {
  86. picker.minimumDate = min;
  87. picker.maximumDate = max;
  88. }else{
  89. picker.minimumDate = minDate;
  90. picker.maximumDate = maxDate;
  91. }
  92. }
  93. -(void)setMaxDate:(NSDate *)maxDate MinDate:(NSDate *)minDate
  94. {
  95. max = maxDate;
  96. min = minDate;
  97. }
  98. -(void)yesClick
  99. {
  100. if (complete) {
  101. NSDateFormatter* fm = [NSDateFormatter new];
  102. if (0 == _style) {
  103. [fm setDateFormat:@"yyyy-MM-dd"];
  104. }else if (1 == _style || 3 == _style){
  105. [fm setDateFormat:@"HH:mm"];
  106. }else if (2 == _style) {
  107. [fm setDateFormat:@"EEE yyyy-MM-dd"];
  108. }
  109. complete([fm stringFromDate:picker.date]);
  110. [self removeFromSuperview];
  111. }
  112. }
  113. -(void)showWithComplete:(MyBlockType)comp
  114. {
  115. complete = comp;
  116. [[UIApplication sharedApplication].keyWindow addSubview:self];
  117. }
  118. @end