DateView.m 4.2 KB

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