cordova-plugin-datepicker插件是一款选择日期时间的插件,底层ios端使用的是UIDatePicker控件。
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
UIDatePickerModeTime, // 只显示时间
UIDatePickerModeDate, // 只显示日期
UIDatePickerModeDateAndTime, // 显示日期和时间
UIDatePickerModeCountDownTimer // 只显示小时和分钟 倒计时定时器
};
但是该原生控件只提供了4种显示模式:时间、日期、日期+时间、倒计时。
目前需求需要一种可以只选择年月的模式。
所以在底层进行修改的时候,只能重新用UIPickerView将这一种模式单独和其他方法区别开
在原有的方法中做判断,如果是需求的模式则不再进showForPhone方法了。
界面搭建
-(void)showForRequiredMode:(NSMutableDictionary*)options {
CGFloatwidth;
CGFloatheight;
UIInterfaceOrientationdeviceOrientation = [UIApplicationsharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsLandscape(deviceOrientation)){
width =self.webView.superview.frame.size.width;
height=self.webView.superview.frame.size.height;
}else{
width =self.webView.superview.frame.size.width;
height=self.webView.superview.frame.size.height;
}
UIView*requiredContainer = [[UIViewalloc]initWithFrame:CGRectMake(0,0, width, height)];
//self.requiredContainer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
self.requiredContainer= requiredContainer;
[self.webView.superviewaddSubview:requiredContainer];
UIView*functionContainer = [[UIViewalloc]initWithFrame:CGRectMake(0,0, width, height)];
//functionContainer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
functionContainer.backgroundColor= [UIColorclearColor];
[requiredContaineraddSubview:functionContainer];
functionContainer.userInteractionEnabled=YES;
//界面主体日期选择
UIPickerView*requiredPickerView = [[UIPickerViewalloc]initWithFrame:CGRectMake(0, height-170, width,170)];
requiredPickerView.backgroundColor= [UIColorwhiteColor];
//[requiredContainer addSubview:requiredPickerView];
[functionContaineraddSubview:requiredPickerView];
requiredPickerView.showsSelectionIndicator=YES;
requiredPickerView.delegate=self;
requiredPickerView.dataSource=self;
requiredPickerView.userInteractionEnabled=YES;
self.requiredPickerView= requiredPickerView;
//确定取消button
UIButton*requiredCancelButton = [[UIButtonalloc]initWithFrame:CGRectMake(0, height-216, width *0.5,46)];
UIButton*requiredDoneButton = [[UIButtonalloc]initWithFrame:CGRectMake(width *0.5, height-216, width *0.5,46)];
[requiredCancelButtonsetTitle:@"取消"forState:UIControlStateNormal];
[requiredDoneButtonsetTitle:@"完成"forState:UIControlStateNormal];
requiredCancelButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
requiredDoneButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
[requiredCancelButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[requiredDoneButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
requiredCancelButton.contentEdgeInsets=UIEdgeInsetsMake(0,10,0,0);
requiredDoneButton.contentEdgeInsets=UIEdgeInsetsMake(0,0,0,10);
requiredCancelButton.backgroundColor= [UIColorwhiteColor];
requiredDoneButton.backgroundColor= [UIColorwhiteColor];
//[requiredContainer addSubview:requiredCancelButton];
//[requiredContainer addSubview:requiredDoneButton];
requiredCancelButton.userInteractionEnabled=YES;
requiredDoneButton.userInteractionEnabled=YES;
[functionContaineraddSubview:requiredCancelButton];
[functionContaineraddSubview:requiredDoneButton];
[requiredCancelButtonaddTarget:selfaction:@selector(requiredCancenButtonClick)forControlEvents:UIControlEventTouchDown];
[requiredDoneButtonaddTarget:selfaction:@selector(requiredDoneButtonClick)forControlEvents:UIControlEventTouchDown];
CGRectframe = functionContainer.frame;
functionContainer.frame=CGRectOffset(frame,0, frame.size.height);
functionContainer.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0];
[UIViewanimateWithDuration:ANIMATION_DURATIONdelay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{
//functionContainer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
functionContainer.frame= frame;
requiredContainer.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0.4];
}completion:^(BOOLfinished) {
}];
}
年、月数据的设置
-(void)initPickerView:(NSMutableDictionary*)options{
self.yearArr= [NSMutableArrayarrayWithCapacity:200];
for(inti =0; i <200; i++) {
[_yearArraddObject:[NSStringstringWithFormat:@"%d年",START_YEAR+i]];
}
self.monthArr= [NSMutableArrayarrayWithCapacity:12];
for(inti =1; i <=12; i ++) {
[_monthArraddObject:[NSStringstringWithFormat:@"%d月",i]];
}
[selfpointToToday];
}
以下是为了做一些校验: 今天之前、今天之后的限制
-(void) updateRequiredDatePicker:(NSMutableDictionary*)options {
self.allowOldDates= ([[optionsobjectForKey:@"allowOldDates"]intValue] ==0) ?NO:YES;
self.allowFutureDates= ([[optionsobjectForKey:@"allowFutureDates"]intValue] ==0) ?NO:YES;
}
UIPickerView的delegate 和 dataSource 方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
return2;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component {
if(component ==0) {
returnself.yearArr.count;
}else{
returnself.monthArr.count;
}
}
-(CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
return200;
}
-(CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component {
return30;
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(component ==0) {
return[self.yearArrobjectAtIndex:row];
}else{
return[self.monthArrobjectAtIndex:row];
}
}
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(component ==0) {
intselectedYearNow = (int)row + (int)START_YEAR;
if(!self.allowOldDates) {
if(selectedYearNow
[self.requiredPickerViewselectRow:(self.yearNow-START_YEAR)inComponent:0animated:NO];
}else{
self.currentSelectYear= selectedYearNow;
}
}elseif(!self.allowFutureDates){
if(selectedYearNow >self.yearNow) {
[self.requiredPickerViewselectRow:(self.yearNow-START_YEAR)inComponent:0animated:NO];
}else{
self.currentSelectYear= selectedYearNow;
}
}else{
self.currentSelectYear= selectedYearNow;
}
}else{
//self.currentSelectMonth = (int)row + 1;
intselectedMonthNow = (int)row +1;
if(!self.allowOldDates) {
if(selectedMonthNow
[self.requiredPickerViewselectRow:(self.monthNow-1)inComponent:1animated:NO];
}else{
self.currentSelectMonth= selectedMonthNow;
}
}elseif(!self.allowFutureDates){
if(selectedMonthNow >self.monthNow) {
[self.requiredPickerViewselectRow:(self.monthNow-1)inComponent:1animated:NO];
}else{
self.currentSelectMonth= selectedMonthNow;
}
}else{
self.currentSelectMonth= selectedMonthNow;
}
}
}
粗略做了一下,界面不是太美观,不过和插件的差不多了~~
网友评论