123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #import "DateView.h"
- @implementation DateView
- -(instancetype)init
- {
- self = [super initWithFrame:kFrame];
- if (self) {
- [self setBackgroundColor:[UIColor colorWithWhite:.01 alpha:.3]];
- CGFloat x,y,w,h,bd;
-
- x = bd = 10;
- y = 0;
- w = kSize.width - bd*2;
- h = 300;
-
- UIView* aView;
- aView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [aView setBackgroundColor:backGroundColor];
- aView.center = CGPointMake(kSize.width/2.0, kSize.height/2.0);
- [self addSubview:aView];
-
- UILabel* label;
- x = bd;
- h = 40;
- w = aView.width - bd*2;
- label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w , h)];
- [label setText:@"请选择时间"];
- [label setTextAlignment:NSTextAlignmentCenter];
- [aView addSubview:label];
-
- UIView* line;
- y += h;
- h = 3;
- x = bd;
- line = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [aView addSubview:line];
- [line setBackgroundColor:RQ_MAIN_COLOR];
-
- y += h;
- h = 200;
- picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(x, y, w , h)];
- [picker setDatePickerMode:UIDatePickerModeDate];
- if (@available(iOS 13.4, *)) {
- picker.preferredDatePickerStyle = UIDatePickerStyleWheels;
- } else {
- // Fallback on earlier versions
- }
- [aView addSubview:picker];
-
-
- x = 60;
- y += h + bd;
- w = (kSize.width- bd*2 - 60*3)/2.0;
- h = aView.height - y - bd;
-
- UIButton* btn;
- btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [btn setTitle:@"取消" forState:UIControlStateNormal];
- [btn setBackgroundColor:RQ_MAIN_COLOR];
- [btn addTarget:self action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
- [aView addSubview:btn];
- btn.layer.cornerRadius = btn.height*.5;
- btn.layer.masksToBounds = YES;
-
- x += w+60;
- btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [btn setTitle:@"确定" forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(yesClick) forControlEvents:UIControlEventTouchUpInside];
- [btn setBackgroundColor:RQ_MAIN_COLOR];
- [aView addSubview:btn];
- btn.layer.cornerRadius = btn.height*.5;
- btn.layer.masksToBounds = YES;
- }
- return self;
- }
- -(void)setStyle:(int)style
- {
- _style = style;
- NSDateFormatter * formatter = [[ NSDateFormatter alloc ] init ];
- NSString * mindateStr ;
- NSString * maxdateStr;
- if (0 == _style || 2 == _style) {
- [picker setDatePickerMode:UIDatePickerModeDate];
- [formatter setDateFormat : @"yyyy"];
- int year = [formatter stringFromDate:[NSDate date]].intValue;
- [formatter setDateFormat : @"yyyy-MM-dd"];
- mindateStr = [NSString stringWithFormat:@"%d-01-01",year];
- maxdateStr = [NSString stringWithFormat:@"%d-12-31",year + 2];
- }
- if (1 == _style) {
- [picker setDatePickerMode:UIDatePickerModeTime];
- }
-
- if (3 == _style) {
- [picker setDatePickerMode:UIDatePickerModeTime];
- //这个是设置分钟轮上面时间间隔的
- picker.minuteInterval = 30;
- }
-
- NSDate * minDate = [formatter dateFromString :mindateStr];
- NSDate * maxDate = [formatter dateFromString :maxdateStr];
-
- if (maxDate) {
- picker.minimumDate = min;
- picker.maximumDate = max;
- }else{
- picker.minimumDate = minDate;
- picker.maximumDate = maxDate;
- }
- }
- -(void)setMaxDate:(NSDate *)maxDate MinDate:(NSDate *)minDate
- {
- max = maxDate;
- min = minDate;
- }
- -(void)yesClick
- {
- if (complete) {
- NSDateFormatter* fm = [NSDateFormatter new];
- fm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
- if (0 == _style) {
- [fm setDateFormat:@"yyyy-MM-dd"];
- }else if (1 == _style || 3 == _style){
- [fm setDateFormat:@"HH:mm"];
- }else if (2 == _style) {
- [fm setDateFormat:@"EEE yyyy-MM-dd"];
- }
- complete([fm stringFromDate:picker.date]);
- [self removeFromSuperview];
- }
- }
- -(void)showWithComplete:(MyBlockType)comp
- {
- complete = comp;
- [[UIApplication sharedApplication].keyWindow addSubview:self];
- }
- @end
|