From 4df5d6953b686f387e0cddf2ca76b43e7052ed48 Mon Sep 17 00:00:00 2001 From: luckychunxiao Date: Tue, 20 Oct 2015 18:04:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0WMDateStyle=5FYearMonthDayHou?= =?UTF-8?q?r=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加WMDateStyle_YearMonthDayHour 模式,处理时差问题,添加Toolbar --- datePicker/ViewController.m | 3 +- .../WMCoustomDatepicker/WMCustomDatePicker.h | 2 + .../WMCoustomDatepicker/WMCustomDatePicker.m | 121 +++++++++++++++++- 3 files changed, 119 insertions(+), 7 deletions(-) diff --git a/datePicker/ViewController.m b/datePicker/ViewController.m index b818b38..083a14d 100644 --- a/datePicker/ViewController.m +++ b/datePicker/ViewController.m @@ -40,9 +40,10 @@ - (void)viewDidLoad { 武猛创作 (参考uudate的源代码 )在此感谢uuDatepicker的作者 */ - WMCustomDatePicker *picker = [[WMCustomDatePicker alloc]initWithframe:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300) Delegate:self PickerStyle:WMDateStyle_YearMonthDay]; + WMCustomDatePicker *picker = [[WMCustomDatePicker alloc]initWithframe:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300) Delegate:self PickerStyle:WMDateStyle_YearMonthDayHour]; picker.minLimitDate = [NSDate date]; picker.maxLimitDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60*30*12]; + picker.hasToolbar = YES; _TF.inputView = picker; /* diff --git a/datePicker/WMCoustomDatepicker/WMCustomDatePicker.h b/datePicker/WMCoustomDatepicker/WMCustomDatePicker.h index f91c69b..fb49a50 100644 --- a/datePicker/WMCoustomDatepicker/WMCustomDatePicker.h +++ b/datePicker/WMCoustomDatepicker/WMCustomDatePicker.h @@ -10,6 +10,7 @@ typedef enum{ WMDateStyle_YearMonthDayHourMinute = 0, + WMDateStyle_YearMonthDayHour, WMDateStyle_YearMonthDay, WMDateStyle_MonthDayHourMinute, WMDateStyle_HourMinute @@ -37,6 +38,7 @@ typedef enum{ @interface WMCustomDatePicker : UIView @property (nonatomic, assign)iddelegate; @property (nonatomic, assign) WMDateStyle datePickerStyle; +@property (nonatomic, assign) BOOL hasToolbar; //Toolbar中含完成按钮 @property (nonatomic, strong) NSDate *ScrollToDate;//滚到指定日期 @property (nonatomic, strong) NSDate *maxLimitDate;//限制最大时间(没有设置默认2049) @property (nonatomic, strong) NSDate *minLimitDate;//限制最小时间(没有设置默认1970) diff --git a/datePicker/WMCoustomDatepicker/WMCustomDatePicker.m b/datePicker/WMCoustomDatepicker/WMCustomDatePicker.m index bb19577..d1cb50d 100644 --- a/datePicker/WMCoustomDatepicker/WMCustomDatePicker.m +++ b/datePicker/WMCoustomDatepicker/WMCustomDatePicker.m @@ -19,6 +19,8 @@ #define DATEPICKER_interval 1//设置分钟时间间隔 #define DATEMAXFONT 20 //修改最大的文字大小 +#define DATEPICKER_TOOLBAR_HEIGTH 40 + #define DATE_GRAY [UIColor redColor]; #define DATE_BLACK [UIColor blackColor]; #define WMSCREENWEIGHT [UIScreen mainScreen].bounds.size.width @@ -126,13 +128,31 @@ -(void)reloadViewsAndData //获取当前日期,储存当前时间位置 NSArray *indexArray = [self getNowDate:self.ScrollToDate]; myPickerView = nil; - myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; + if (_hasToolbar) + myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, DATEPICKER_TOOLBAR_HEIGTH, self.frame.size.width, self.frame.size.height-DATEPICKER_TOOLBAR_HEIGTH)]; + else + myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; myPickerView.showsSelectionIndicator = YES; myPickerView.backgroundColor = [UIColor clearColor]; myPickerView.delegate = self; myPickerView.dataSource = self; [self addSubview:myPickerView]; + if (_hasToolbar) { + //添加TOOLBAR + UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, DATEPICKER_TOOLBAR_HEIGTH)]; + [self addSubview:toolBar]; + NSMutableArray *barButtonItems = [[NSMutableArray alloc]initWithCapacity:1]; + UIBarButtonItem *toolBarDoneItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerViewEndEdit)]; + UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace + target:self + action:NULL]; + [barButtonItems addObject:flexibleSpaceItem]; + [barButtonItems addObject:toolBarDoneItem]; + [toolBar setItems:barButtonItems animated:NO]; + } + //调整为现在的时间 for (int i=0; i