仿了一个DatePickerView,使用就在响应方法里面初始化就好.代码直接贴在下面了
TimePickerView.h
#import
typedef enum: NSUInteger {
DatePickerViewDateTimeModel,//年月日时分
DatePickerViewDateModel,//年月日
DatePickerViewTimeModel,//时分
} DatePickerViewModel;
NS_ASSUME_NONNULL_BEGIN
@protocolTimePickerViewDelegate
//确定按钮
- (void)didClickFinishDateTimePickerView:(NSString*)date;
//取消按钮
- (void)didClickCancelTimePickerView;
@end
@interfaceTimePickerView :UIView
/** 协议 */
@property (nonatomic, weak) id<TimePickerViewDelegate>delegate;
//当前时间
@property (nonatomic, strong) NSDate *currentDate;
//标题中心文字
@property (nonatomic, strong) UILabel *titleLabel;
//时间选择器模式
@property (nonatomic, assign) DatePickerViewModel pickerViewModel;
//隐藏
- (void)hideTimePickerView;
//显示
- (void)showTimePickerView;
@end
TimePickerView.m
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue &0xFF0000) >>16))/255.0green:((float)((rgbValue &0xFF00) >>8))/255.0blue:((float)(rgbValue &0xFF))/255.0alpha:1.0]
//获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0fgreen:(g)/255.0fblue:(b)/255.0falpha:a]
@interface TimePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
NSInteger yearRange;
NSInteger dayRange;
NSInteger startYear;
NSInteger selectedYear;
NSInteger selectedMonth;
NSInteger selectedDay;
NSInteger selectedHour;
NSInteger selectedMinute;
NSInteger selectedSecond;
NSCalendar *calendar;
//左边按钮
UIButton *cancelButton;
//右边按钮
UIButton *chooseButton;
}
/** 选择器 */
@property (nonatomic, strong) UIPickerView *pickerView;
/** 选择器内容 */
@property (nonatomic, copy) NSString *string;
@property (nonatomic, strong) UIView *contentV;
@property (nonatomic, strong) UIView *bgView;
@end
@implementationTimePickerView
- (instancetype)initWithFrame:(CGRect)frame
{
if(self= [super initWithFrame:frame]) {
self.backgroundColor=RGBA(0,0,0,0.5);
self.alpha=0;
UIView *contentV = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 220)];
contentV.backgroundColor = UIColor.whiteColor;
[self addSubview:contentV];
self.contentV= contentV;
self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 180)];
self.pickerView.backgroundColor = UIColor.whiteColor;
self.pickerView.delegate=self;
self.pickerView.dataSource=self;
[contentV addSubview:self.pickerView];
//选择器上方view
UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
topView.backgroundColor = UIColor.whiteColor;
[contentV addSubview:topView];
//取消按钮
cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame=CGRectMake(12,0,40,40);
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:UIColorFromRGB(0x0d8bf5) forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
[topView addSubview:cancelButton];
//确定按钮
chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
chooseButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-52, 0, 40, 40);
[chooseButton setTitle:@"确定" forState:UIControlStateNormal];
[chooseButton setTitleColor:UIColorFromRGB(0x0d8bf5) forState:UIControlStateNormal];
[chooseButton addTarget:self action:@selector(chooseButtonClick) forControlEvents:UIControlEventTouchUpInside];
[topView addSubview:chooseButton];
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cancelButton.frame), 0, SCREEN_WIDTH-104, 40)];
[topView addSubview:self.titleLabel];
_titleLabel.textColor=UIColorFromRGB(0x3f4548);
_titleLabel.font = [UIFont systemFontOfSize:13];
_titleLabel.textAlignment = NSTextAlignmentCenter;
//分割线
UIView *splitView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 0.5)];
splitView.backgroundColor=UIColorFromRGB(0xe6e6e6);
[topView addSubview:splitView];
NSCalendar *calendar0 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc]init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
comps = [calendar0 components:unitFlags fromDate:[NSDate date]];
NSInteger year = [compsyear];
startYear= year -15;
yearRange=50;
[self setCurrentDate:[NSDate date]];
}
return self;
}
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
return5;
}else if (self.pickerViewModel == DatePickerViewDateModel) {
return3;
}else if (self.pickerViewModel == DatePickerViewTimeModel) {
return2;
}
return0;
}
//每列返回的内容
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
switch(component) {
case 0:
return yearRange;
break;
case1:
return 12;
break;
case 2:
return dayRange;
break;
case 3:
return 24;
break;
case 4:
return 60;
break;
default:
break;
}
}else if (self.pickerViewModel == DatePickerViewDateModel){
switch(component) {
case 0:
return yearRange;
break;
case 1:
return 12;
break;
case 2:
return dayRange;
break;
default:
break;
}
}else if (self.pickerViewModel == DatePickerViewTimeModel){
switch(component) {
case 0:
return 24;
break;
case 1:
return 60;
break;
default:
break;
}
}
return 0;
}
#pragma mark UIPickerViewDelegate
//默认时间处理
- (void)setCurrentDate:(NSDate*)currentDate{
//获取当前时间
NSCalendar *calendar0 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc]init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
comps = [calendar0 components:unitFlags fromDate:[NSDate date]];
NSInteger year = [comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
selectedYear= year;
selectedMonth= month;
selectedDay= day;
selectedHour= hour;
selectedMinute= minute;
dayRange = [self isAllDay:year andMonth:month];
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
[self.pickerView selectRow:year-startYear inComponent:0 animated:NO];
[self.pickerView selectRow:month-1 inComponent:1 animated:NO];
[self.pickerView selectRow:day-1 inComponent:2 animated:NO];
[self.pickerView selectRow:hour inComponent:3 animated:NO];
[self.pickerView selectRow:minute inComponent:4 animated:NO];
[self pickerView:self.pickerView didSelectRow:year-startYear inComponent:0];
[self pickerView:self.pickerView didSelectRow:month-1 inComponent:1];
[self pickerView:self.pickerView didSelectRow:day-1 inComponent:2];
[self pickerView:self.pickerView didSelectRow:hour inComponent:3];
[self pickerView:self.pickerView didSelectRow:minute inComponent:4];
}else if (self.pickerViewModel == DatePickerViewDateModel) {
[self.pickerView selectRow:year-startYear inComponent:0 animated:NO];
[self.pickerView selectRow:month-1 inComponent:1 animated:NO];
[self.pickerView selectRow:day-1 inComponent:2 animated:NO];
[self pickerView:self.pickerView didSelectRow:year-startYear inComponent:0];
[self pickerView:self.pickerView didSelectRow:month-1 inComponent:1];
[self pickerView:self.pickerView didSelectRow:day-1 inComponent:2];
}else if (self.pickerViewModel == DatePickerViewTimeModel) {
[self.pickerView selectRow:hour inComponent:0 animated:NO];
[self.pickerView selectRow:minute inComponent:1 animated:NO];
[self pickerView:self.pickerView didSelectRow:hour inComponent:0];
[self pickerView:self.pickerView didSelectRow:minute inComponent:1];
}
[self.pickerView reloadAllComponents];
}
- (UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view
{
UILabel*label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH* component/6.0,0,SCREEN_WIDTH/6.0,30)];
label.font = [UIFont systemFontOfSize:15.0];
label.tag= component*100+row;
label.textAlignment = NSTextAlignmentCenter;
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
switch(component) {
case 0:
{
label.text= [NSStringstringWithFormat:@"%ld年",(long)(startYear+ row)];
}
break;
case 1:
{
label.text= [NSStringstringWithFormat:@"%ld月",(long)row+1];
}
break;
case 2:
{
label.text= [NSStringstringWithFormat:@"%ld日",(long)row+1];
}
break;
case 3:
{
label.textAlignment = NSTextAlignmentRight;
label.text= [NSStringstringWithFormat:@"%ld时",(long)row];
}
break;
case 4:
{
label.textAlignment = NSTextAlignmentRight;
label.text= [NSStringstringWithFormat:@"%ld分",(long)row];
}
break;
default:
break;
}
}else if (self.pickerViewModel == DatePickerViewDateModel) {
switch(component) {
case 0:
{
label.text= [NSStringstringWithFormat:@"%ld年",(long)(startYear+ row)];
}
break;
case 1:
{
label.text= [NSStringstringWithFormat:@"%ld月",(long)(row+1)];
}
break;
case 2:
{
label.text= [NSStringstringWithFormat:@"%ld日",(long)(row+1)];
}
break;
default:
break;
}
}else if (self.pickerViewModel == DatePickerViewTimeModel) {
switch(component) {
case 0:
{
label.text= [NSStringstringWithFormat:@"%ld时",(long)(row)];
}
break;
case 1:
{
label.text= [NSStringstringWithFormat:@"%ld分",(long)(row)];
}
break;
default:
break;
}
}
returnlabel;
}
//选择内容的宽
- (CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component
{
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
return ([UIScreen mainScreen].bounds.size.width-40)/5;
}else if (self.pickerViewModel == DatePickerViewDateModel) {
return ([UIScreen mainScreen].bounds.size.width-40)/3;
}else if (self.pickerViewModel == DatePickerViewTimeModel) {
return ([UIScreen mainScreen].bounds.size.width-40)/2;
}
return0;
}
//内容的高
- (CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component
{
return 30;
}
// 监听picker的滑动
- (void)pickerView:(UIPickerView*)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (self.pickerViewModel == DatePickerViewDateTimeModel) {
switch(component) {
case 0:
{
selectedYear=startYear+ row;
dayRange=[self isAllDay:selectedYear andMonth:selectedMonth];
[self.pickerView reloadComponent:2];
}
break;
case 1:
{
selectedMonth=row+1;
dayRange=[self isAllDay:selectedYear andMonth:selectedMonth];
[self.pickerView reloadComponent:2];
}
break;
case 2:
{
selectedDay=row+1;
}
break;
case 3:
{
selectedHour=row;
}
break;
case 4:
{
selectedMinute=row;
}
break;
default:
break;
}
_string =[NSString stringWithFormat:@"%ld-%.2ld-%.2ld %.2ld:%.2ld",selectedYear,selectedMonth,selectedDay,selectedHour,selectedMinute];
}else if (self.pickerViewModel == DatePickerViewDateModel){
switch(component) {
case 0:
{
selectedYear=startYear+ row;
dayRange=[self isAllDay:selectedYear andMonth:selectedMonth];
[self.pickerViewreloadComponent:2];
}
break;
case 1:
{
selectedMonth=row+1;
dayRange=[self isAllDay:selectedYear andMonth:selectedMonth];
[self.pickerView reloadComponent:2];
}
break;
case 2:
{
selectedDay=row+1;
}
break;
default:
break;
}
_string =[NSString stringWithFormat:@"%ld-%.2ld-%.2ld",selectedYear,selectedMonth,selectedDay];
}else if (self.pickerViewModel == DatePickerViewTimeModel){
switch(component) {
case 0:
{
selectedHour=row;
}
break;
case 1:
{
selectedMinute=row;
}
break;
default:
break;
}
_string =[NSString stringWithFormat:@"%.2ld:%.2ld",selectedHour,selectedMinute];
}
}
#pragma mark Show Or Hidden
- (void)showTimePickerView
{
[self setCurrentDate:[NSDate date]];
__weak typeof(self)weakSelf = self;
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
[UIView animateWithDuration:0.25f animations:^{
self.alpha=1;
weakSelf.contentV.frame=CGRectMake(0,SCREEN_HEIGHT-220,SCREEN_WIDTH,220);
}completion:^(BOOLfinished) {
}];
}
- (void)hideTimePickerView
{
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.25f animations:^{
self.alpha=0;
weakSelf.contentV.frame=CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH,220);
}completion:^(BOOLfinished) {
self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
}
#pragma mark -- private
//取消方法
- (void)cancelButtonClick{
if(self.delegate!=nil&& [self.delegaterespondsToSelector:@selector(didClickCancelTimePickerView)]) {
[self.delegate didClickCancelTimePickerView];
}
[self hideTimePickerView];
}
//选中方法
- (void)chooseButtonClick{
if(self.delegate!=nil&& [self.delegaterespondsToSelector:@selector(didClickFinishDateTimePickerView:)]) {
[self.delegate didClickFinishDateTimePickerView:_string];
}
[self hideTimePickerView];
}
//每月天数
- (NSInteger)isAllDay:(NSInteger)year andMonth:(NSInteger)month{
intday =0;
//1、3、5、7、8、10、12月31天
switch(month) {
case1:
case3:
case5:
case7:
case8:
case10:
case12:
day =31;
break;
//2、4、5、6、11月30天
case4:
case6:
case9:
case11:
day =30;
break;
//判断平年闰年二月天数
case2:
{
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
day =29;
break;
}else{
day =28;
break;
}
}
default:
break;
}
returnday;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
[self hideTimePickerView];
}
@end