// // DateView.m // jiaPeiC // // Created by apple on 15/12/20. // Copyright © 2015年 JCZ. All rights reserved. // #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:[UIColor whiteColor]]; [aView borderCornorRadios:5]; aView.center = CGPointMake(kSize.width/2.0, kSize.height/2.0); [self addSubview:aView]; UILabel* label; x = bd; y = 0; h = 40; w = aView.width - bd*2; label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w , h)]; [label setText:@"请选择时间"]; [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:RQMianColor]; y += h; h = 200; picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(x, y, w - bd*2, h)]; [picker setDatePickerMode:UIDatePickerModeDate]; [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:RQMianColor]; [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:RQMianColor]; [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) { [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 - 1]; maxdateStr = [NSString stringWithFormat:@"%d-12-31",year + 2]; } if (1 == _style) { [picker setDatePickerMode:UIDatePickerModeTime]; //这个是设置分钟轮上面时间间隔的 picker.minuteInterval = 10; } NSDate *minDate = [formatter dateFromString :mindateStr]; NSDate *maxDate = [formatter dateFromString :maxdateStr]; picker.minimumDate = minDate; picker.maximumDate = maxDate; } -(void)yesClick { if (complete) { NSDateFormatter* fm = [NSDateFormatter new]; if (0 == _style) { [fm setDateFormat:@"yyyy-MM-dd"]; complete([fm stringFromDate:picker.date]); }else{ [fm setDateFormat:@"HH:mm"]; NSString *timeString = [fm stringFromDate:picker.date]; NSMutableString *muString = [NSMutableString stringWithString:timeString]; [muString replaceCharactersInRange:NSMakeRange(4, 1) withString:@"0"]; timeString = (NSString *)muString; complete(timeString); } [self removeFromSuperview]; } } -(void)showWithComplete:(MyBlockType)comp { complete = comp; [[UIApplication sharedApplication].keyWindow addSubview:self]; } @end