// // DatePickerCustomView.m // E-Mobile // // Created by Yang on 15/9/18. // // #import "DatePickerCustomView.h" @interface DatePickerCustomView(){ } @property(nonatomic,strong)UIButton* timePickerCancelBtn; @property(nonatomic,strong)UIView* buttonView; @end @implementation DatePickerCustomView +(instancetype)showInView:(UIView*)view Mode:(UIDatePickerMode)mode date:(NSDate*)date { DatePickerCustomView* customView = [[self alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)]; if (customView) { customView.backgroundColor=RGBA(0, 0, 0, 0.3); UIButton* timePickerCancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; customView.timePickerCancelBtn = timePickerCancelBtn; timePickerCancelBtn.tag = 100; timePickerCancelBtn.frame = customView.bounds; [timePickerCancelBtn addTarget:customView action:@selector(datePickerCancelBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; [customView addSubview:timePickerCancelBtn]; UIDatePicker* datePicker = [[UIDatePicker alloc] initWithFrame: CGRectMake(0, customView.frame.size.height+40, customView.frame.size.width, 216)]; customView.datePicker = datePicker; [datePicker setLocale:[NSLocale autoupdatingCurrentLocale]]; [datePicker setDatePickerMode:mode]; datePicker.backgroundColor=[UIColor whiteColor]; datePicker.tag = 101; if (date) { [datePicker setDate:date]; } // [datePicker addTarget:customView action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged]; [customView addSubview:datePicker]; UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, customView.frame.size.height, customView.frame.size.width, 40)]; customView.buttonView = buttonView; buttonView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]; [customView addSubview:buttonView]; UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)]; [cancelBtn addTarget:customView action:@selector(datePickerCancelBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal]; [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateSelected]; cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [cancelBtn setTitleColor:[UIColor colorWithRed:21.0/255 green:126.0/255 blue:251.0/255 alpha:1] forState:UIControlStateNormal]; [cancelBtn setTitleColor:[UIColor colorWithRed:21.0/255 green:126.0/255 blue:251.0/255 alpha:1] forState:UIControlStateSelected]; [buttonView addSubview:cancelBtn]; UIButton *commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(customView.frame.size.width-60, 0, 60, 40)]; [commitBtn addTarget:customView action:@selector(datePickerCommitBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; [commitBtn setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal]; [commitBtn setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateSelected]; commitBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [commitBtn setTitleColor:[UIColor colorWithRed:21.0/255 green:126.0/255 blue:251.0/255 alpha:1] forState:UIControlStateNormal]; [commitBtn setTitleColor:[UIColor colorWithRed:21.0/255 green:126.0/255 blue:251.0/255 alpha:1] forState:UIControlStateSelected]; [buttonView addSubview:commitBtn]; [customView showDatePickerInView:view]; } return customView; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { /*init code*/ } return self; } -(void)datePickerCancelBtnPressed:(UIButton*)btn{ [UIView animateWithDuration:0.2 animations:^{ self.datePicker.frame = CGRectMake(0, self.frame.size.height+40, self.frame.size.width, 216); self.buttonView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 40); }completion:^(BOOL finished) { if (self.cancelBlock) { self.cancelBlock(); } [self removeFromSuperview]; }]; } -(void)datePickerCommitBtnPressed:(UIButton*)btn { [UIView animateWithDuration:0.2 animations:^{ self.datePicker.frame = CGRectMake(0, self.frame.size.height+40, self.frame.size.width, 216); self.buttonView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 40); }completion:^(BOOL finished) { self.dateChanged(); [self removeFromSuperview]; }]; } -(void)showDatePickerInView:(UIView*)view{ [view addSubview:self]; [UIView animateWithDuration:0.2 animations:^{ self.datePicker.frame = CGRectMake(0, self.frame.size.height-216, self.frame.size.width, 216); self.buttonView.frame = CGRectMake(0, self.frame.size.height-216-40, self.frame.size.width, 40); }]; } -(void)datePickerChanged:(id)sender{ self.dateChanged(); } @end