iOS 时间选择器 支持月 日
来源:互联网 发布:3g网络精灵 编辑:程序博客网 时间:2024/05/21 11:01
直接贴代码吧,有用不到的地方 自己处理删除
@interface CanlendarViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIButton * selectedBtn;
UIView * month_top;
UIView * day_top;
UILabel * month_Lb;
UIButton * dayStart_btn;
UIButton * dayEnd_btn;
UIPickerView * pickerView_month;
UIPickerView * pickerView_day;
NSMutableArray * years_Muarr;
NSMutableArray * months_MuArr;
NSMutableArray * days_Arr;
NSInteger minYear;//最早时间点年
NSInteger minMonth;//最早时间点月
NSInteger minDay;//最早时间点日
NSInteger currentYear;//系统当前年
NSInteger currentMonth;//系统当前月
NSInteger currentDay;//系统当前日
NSString * selectedYear;//当前选中的年
NSString * selectedMonth;//当前选中的月
NSString * selectedDay;//当前选中的日
NSString * selectedYear_Month;//当前选中的年_月选择
}
@end
@implementation CanlendarViewController
- (void)rightItemButtonAction:(UIButton *)button
{
NSString * blockStr = @"";
if (selectedBtn.selected) {
//按日
NSDateFormatter * formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy.MM.dd"];
NSDate * startDate = [formatter dateFromString:dayStart_btn.titleLabel.text];
NSDate * endDate = [formatter dateFromString:dayEnd_btn.titleLabel.text];
NSTimeInterval startTimeInter = [startDatetimeIntervalSince1970];
NSTimeInterval endTimeInter = [endDatetimeIntervalSince1970];
if (startTimeInter > endTimeInter) {
blockStr = [NSStringstringWithFormat:@"%@-%@",dayEnd_btn.titleLabel.text,dayStart_btn.titleLabel.text];
}else{
blockStr = [NSStringstringWithFormat:@"%@-%@",dayStart_btn.titleLabel.text,dayEnd_btn.titleLabel.text];
}
}else{
//按月
blockStr = month_Lb.text;
}
if (self.SelectedBlock) {
self.SelectedBlock(blockStr);
}
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[superviewDidLoad];
minYear = 2016;
minMonth = 1;
minDay = 1;
[selfcurrentYearAndMonthAndDay];
[selfsetRightItemWithFrame:CGRectMake(0,0, 30,40) withImageName:nilwithTitle:@"完成"];
[selfsetLeftItemWithFrame:CGRectMake(0,0, 30,40) withImageName:nilwithTitle:@"取消"];
years_Muarr = [NSMutableArrayarrayWithCapacity:0];
months_MuArr = [NSMutableArrayarrayWithCapacity:0];
days_Arr = [NSMutableArrayarrayWithCapacity:0];
[selfcalculationYears];
[selfcalculationMonthsAndDays];
self.title =@"选择时间";
[self setup];
dayStart_btn.selected =YES;//默认开启起始日选中
}
- (void)calculationYears
{
if (minYear ==currentYear) {
[years_MuarraddObject:[NSStringstringWithFormat:@"%ld年",currentYear]];
}else{
for (int i = (int)minYear; i <= (int)currentYear; i ++) {
[years_MuarraddObject:[NSStringstringWithFormat:@"%d年",i]];
}
}
}
- (void)currentYearAndMonthAndDay
{
NSDate * nowDate = [NSDatedate];
NSDateFormatter * formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy.MM.dd"];
NSString * canlendarStr = [formatter stringFromDate:nowDate];
NSArray * canlendarArr = [canlendarStr componentsSeparatedByString:@"."];
currentYear = [canlendarArr[0]integerValue];
currentMonth = [canlendarArr[1]integerValue];
currentDay = [canlendarArr[2]integerValue];
selectedYear_Month = canlendarArr[0];
selectedYear = canlendarArr[0];
selectedMonth = canlendarArr[1];
selectedDay = canlendarArr[2];
}
- (void)setup
{
selectedBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
selectedBtn.frame =CGRectMake(15,30.5, 107, 27);
[selectedBtnsetTitle:@"按月选择" forState:UIControlStateNormal];
[selectedBtnsetTitle:@"按日选择" forState:UIControlStateSelected];
[selectedBtnsetImage:[UIImageimageNamed:@"changeBtn_canlendar"]forState:UIControlStateNormal];
[selectedBtnsetImage:[UIImageimageNamed:@"changeBtn_canlendar"]forState:UIControlStateSelected];
[selectedBtnsetTitleColor:UIColorWithRGBA(51,51, 51) forState:UIControlStateNormal];
[selectedBtnsetTitleColor:UIColorWithRGBA(51,51, 51) forState:UIControlStateSelected];
[selectedBtn.titleLabelsetFont:systemFont(13)];
[selectedBtnsetImageEdgeInsets:UIEdgeInsetsMake(7,107 - 17 - 15,7, 17)];
[selectedBtnsetTitleEdgeInsets:UIEdgeInsetsMake(7, -40, 7, 0)];
selectedBtn.layer.cornerRadius =13.5;
selectedBtn.layer.borderColor =UIColorWithRGBA(209,209, 209).CGColor;
selectedBtn.layer.borderWidth =1.0f;
[selectedBtnaddTarget:selfaction:@selector(selectedType:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:selectedBtn];
[selfcreatDataPickerView];
}
- (void)creatDataPickerView
{
if (!month_top) {
month_top = [[UIViewalloc]initWithFrame:CGRectMake(15,97, SCREEN_WIDTH - 30, 30)];
month_top.backgroundColor = [UIColorclearColor];
[self.viewaddSubview:month_top];
month_Lb = [UILabelcreatLabelWithtext:UIColorWithRGBA(51,51, 51) font:systemFont(16)text:[NSStringstringWithFormat:@"%ld.%02ld",currentYear,currentMonth]];
month_Lb.center =CGPointMake(month_top.center.x,15);
UIView * lineV = [[UIViewalloc]initWithFrame:CGRectMake(0,29.5, month_top.width,0.5)];
lineV.backgroundColor = UIColorWithRGBA(51, 51, 51);
[month_topaddSubview:month_Lb];
[month_top addSubview:lineV];
}
if (!day_top) {
day_top = [[UIViewalloc]initWithFrame:CGRectMake(15,97, SCREEN_WIDTH - 30, 30)];
day_top.backgroundColor = [UIColorclearColor];
[self.viewaddSubview:day_top];
NSString * currentCanlendarStr = [NSStringstringWithFormat:@"%ld.%02ld.%02ld",currentYear,currentMonth,currentDay];
dayStart_btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
dayStart_btn.frame =CGRectMake(0,0, day_top.width/2 -17.5, day_top.height);
[dayStart_btnsetTitleColor:UIColorWithRGBA(209,209, 209) forState:UIControlStateNormal];
[dayStart_btnsetTitleColor:UIColorWithRGBA(67,212, 183) forState:UIControlStateSelected];
dayStart_btn.titleLabel.font =systemFont(16);
[dayStart_btnaddTarget:selfaction:@selector(selectDayCanlendar:)forControlEvents:UIControlEventTouchUpInside];
[dayStart_btnsetTitle:currentCanlendarStr forState:UIControlStateNormal];
[dayStart_btnsetTitle:currentCanlendarStr forState:UIControlStateSelected];
[day_top addSubview:dayStart_btn];
dayEnd_btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
dayEnd_btn.frame =CGRectMake(day_top.width/2 +17.5, 0, day_top.width/2 -17.5, day_top.height);
[dayEnd_btnsetTitleColor:UIColorWithRGBA(209,209, 209) forState:UIControlStateNormal];
[dayEnd_btnsetTitleColor:UIColorWithRGBA(67,212, 183) forState:UIControlStateSelected];
dayEnd_btn.titleLabel.font =systemFont(16);
[dayEnd_btnaddTarget:selfaction:@selector(selectDayCanlendar:)forControlEvents:UIControlEventTouchUpInside];
[dayEnd_btnsetTitle:currentCanlendarStr forState:UIControlStateNormal];
[dayEnd_btnsetTitle:currentCanlendarStr forState:UIControlStateSelected];
[day_top addSubview:dayEnd_btn];
UIView * lineV_left = [[UIViewalloc]initWithFrame:CGRectMake(0,29.5, day_top.width/2 -17.5, 0.5)];
lineV_left.backgroundColor = UIColorWithRGBA(51, 51, 51);
UIView * lineV_right = [[UIViewalloc]initWithFrame:CGRectMake(day_top.width/2 + 17.5, 29.5,day_top.width/2 -17.5, 0.5)];
lineV_right.backgroundColor = UIColorWithRGBA(51, 51, 51);
UILabel * label = [UILabelcreatLabelWithtext:UIColorWithRGBA(51,51, 51) font:systemFont(18)text:@"至"];
label.center = CGPointMake(day_top.width/2,15);
[day_top addSubview:dayStart_btn];
[day_top addSubview:dayEnd_btn];
[day_top addSubview:lineV_left];
[day_top addSubview:lineV_right];
[day_top addSubview:label];
day_top.hidden =YES;
}
if (!pickerView_month) {
pickerView_month = [[UIPickerViewalloc]initWithFrame:CGRectMake(0,month_top.bottom +37, SCREEN_WIDTH, 230.5)];
pickerView_month.backgroundColor = [UIColorclearColor];
pickerView_month.delegate =self;
pickerView_month.dataSource =self;
pickerView_month.showsSelectionIndicator =YES;
[self.viewaddSubview:pickerView_month];
[pickerView_monthselectRow:years_Muarr.count -1inComponent:0animated:YES];
[pickerView_monthselectRow:months_MuArr.count -1inComponent:1animated:YES];
}
if (!pickerView_day) {
pickerView_day = [[UIPickerViewalloc]initWithFrame:CGRectMake(0,day_top.bottom +37, SCREEN_WIDTH, 230.5)];
pickerView_day.backgroundColor = [UIColorclearColor];
pickerView_day.delegate =self;
pickerView_day.dataSource =self;
pickerView_day.showsSelectionIndicator =YES;
pickerView_day.hidden =YES;
[self.viewaddSubview:pickerView_day];
[pickerView_dayselectRow:years_Muarr.count -1 inComponent:0animated:YES];
[pickerView_dayselectRow:months_MuArr.count -1 inComponent:1animated:YES];
[pickerView_dayselectRow:days_Arr.count -1 inComponent:2animated:YES];
}
}
- (void)selectedType:(UIButton *)btn
{
btn.selected = !btn.selected;
if (btn.selected) {
//日
day_top.hidden =NO;
month_top.hidden =YES;
pickerView_month.hidden =YES;
pickerView_day.hidden =NO;
}else{
//月
day_top.hidden =YES;
month_top.hidden =NO;
pickerView_month.hidden =NO;
pickerView_day.hidden =YES;
}
}
- (void)selectDayCanlendar:(UIButton *)btn
{
[btn setTitleColor:UIColorWithRGBA(51,51, 51) forState:UIControlStateNormal];
if ([btn isEqual:dayStart_btn]) {
dayStart_btn.selected =YES;
dayEnd_btn.selected =NO;
}else{
dayStart_btn.selected =NO;
dayEnd_btn.selected =YES;
}
}
- (void)calculationMonthsAndDays
{
NSDateFormatter * formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy.MM"];
//获取date
NSDate * date = [formatterdateFromString:[NSStringstringWithFormat:@"%@.%@",selectedYear,selectedMonth]];
//日历
NSCalendar * calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange range = [calendarrangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:date];
NSInteger days = range.length;
[months_MuArrremoveAllObjects];
NSInteger months = 12;
if ([selectedYearintegerValue] ==currentYear) {
months = currentMonth;
if ([selectedMonthintegerValue] >=currentMonth) {
days = currentDay;
}
}
int num = 0;
if ([selectedYearintegerValue] ==minYear) {
num = (int)minMonth -1;
}
for (int i = num; i < months; i++) {
[months_MuArraddObject:[NSStringstringWithFormat:@"%d月",i+1]];
}
[days_ArrremoveAllObjects];
for (int i =0; i < days; i++) {
[days_ArrinsertObject:[NSStringstringWithFormat:@"%d日",i+1]atIndex:i];
}
if (pickerView_day) {
[pickerView_dayreloadComponent:1];
[pickerView_dayreloadComponent:2];
}
}
- (void)calculationMonthsforMonth
{
NSDateFormatter * formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy"];
//获取date
NSDate * date = [formatterdateFromString:[NSStringstringWithFormat:@"%@",selectedYear_Month]];
//日历
NSCalendar * calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange range = [calendarrangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:date];
NSInteger days = range.length;
[months_MuArrremoveAllObjects];
NSInteger months = 12;
if ([selectedYear_MonthintegerValue] ==currentYear) {
months = currentMonth;
}
int num = 0;
if ([selectedYear_MonthintegerValue] ==minYear) {
num = (int)minMonth -1;
}
for (int i = num; i < months; i++) {
[months_MuArraddObject:[NSStringstringWithFormat:@"%d月",i+1]];
}
if (pickerView_month) {
[pickerView_monthreloadComponent:1];
}
}
#pragma -----------------UIPickerViewDelegate,UIPickerDataSource----------
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if ([pickerView isEqual:pickerView_month]) {
return 2;
}
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
returnyears_Muarr.count;
}else if (component ==1) {
returnmonths_MuArr.count;
}else{
return days_Arr.count;
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30.0f;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return years_Muarr[row];
}else if (component ==1) {
return months_MuArr[row];
}else{
return days_Arr[row];
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView isEqual:pickerView_month]) {
if (component == 0) {
selectedYear_Month = [years_Muarr[row]substringToIndex:4];
[selfcalculationMonthsforMonth];
[selfcalculationCanlendarWithYear:years_Muarr[row]Month:months_MuArr[[pickerViewselectedRowInComponent:1]]Day:@""pickerView:pickerView_month];
}else if (component ==1) {
[selfcalculationCanlendarWithYear:years_Muarr[[pickerViewselectedRowInComponent:0]]Month:months_MuArr[row]Day:@""pickerView:pickerView_month];
}
}else{
if (component == 0) {
selectedYear = [years_Muarr[row]substringToIndex:4];
if ([months_MuArr[[pickerViewselectedRowInComponent:1]]length] == 2) {
selectedMonth = [NSStringstringWithFormat:@"%02d",[[months_MuArr[[pickerViewselectedRowInComponent:1]]substringToIndex:1]intValue]];
}else{
selectedMonth = [months_MuArr[[pickerViewselectedRowInComponent:1]]substringToIndex:2];
}
[selfcalculationMonthsAndDays];
[selfcalculationCanlendarWithYear:years_Muarr[row]Month:months_MuArr[[pickerViewselectedRowInComponent:1]]Day:days_Arr[[pickerViewselectedRowInComponent:2]]pickerView:pickerView_day];
}else if (component ==1) {
if ([months_MuArr[row]length] == 2) {
selectedMonth = [NSStringstringWithFormat:@"%02d",[[months_MuArr[row]substringToIndex:1]intValue]];
}else{
selectedMonth = [months_MuArr[row]substringToIndex:2];
}
[selfcalculationMonthsAndDays];
[selfcalculationCanlendarWithYear:years_Muarr[[pickerViewselectedRowInComponent:0]]Month:months_MuArr[row]Day:days_Arr[[pickerViewselectedRowInComponent:2]]pickerView:pickerView_day];
}else{
[selfcalculationCanlendarWithYear:years_Muarr[[pickerViewselectedRowInComponent:0]]Month:months_MuArr[[pickerViewselectedRowInComponent:1]]Day:days_Arr[row]pickerView:pickerView_day];
}
}
}
- (void)calculationCanlendarWithYear:(NSString *)yearStr Month:(NSString *)monthStr Day:(NSString *)dayStr pickerView:(UIPickerView *)pickerView
{
if ([pickerView isEqual:pickerView_month]) {
if (yearStr.length >0) {
month_Lb.text = [month_Lb.textstringByReplacingCharactersInRange:NSMakeRange(0,4) withString:[yearStrsubstringToIndex:4]];
}
if (monthStr.length >0) {
if (monthStr.length ==2) {
monthStr = [NSStringstringWithFormat:@"%02d",[[monthStrsubstringToIndex:1]intValue]];
}else{
monthStr = [monthStr substringToIndex:2];
}
month_Lb.text = [month_Lb.textstringByReplacingCharactersInRange:NSMakeRange(5,2) withString:monthStr];
}
[month_Lb sizeToFit];
month_Lb.center =CGPointMake(month_top.center.x,month_Lb.center.y);
}else{
if (dayStart_btn.selected) {
if (yearStr.length >0) {
NSString * textStr = [dayStart_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(0,4) withString:[yearStrsubstringToIndex:4]];
[dayStart_btnsetTitle:textStr forState:UIControlStateSelected];
[dayStart_btnsetTitle:textStr forState:UIControlStateNormal];
}
if (monthStr.length >0) {
if (monthStr.length ==2) {
monthStr = [NSString stringWithFormat:@"%02d",[[monthStr substringToIndex:1] intValue]];
}else{
monthStr = [monthStr substringToIndex:2];
}
NSString * textStr = [dayStart_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(5,2) withString:monthStr];
[dayStart_btnsetTitle:textStr forState:UIControlStateSelected];
[dayStart_btnsetTitle:textStr forState:UIControlStateNormal];
}
if (dayStr.length >0) {
if (dayStr.length ==2) {
dayStr = [NSStringstringWithFormat:@"%02d",[[dayStrsubstringToIndex:1]intValue]];
}else{
dayStr = [dayStr substringToIndex:2];
}
NSString * textStr = [dayStart_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(8,2) withString:dayStr];
[dayStart_btnsetTitle:textStr forState:UIControlStateSelected];
[dayStart_btnsetTitle:textStr forState:UIControlStateNormal];
}
}else{
if (yearStr.length >0) {
NSString * textStr = [dayEnd_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(0,4) withString:[yearStrsubstringToIndex:4]];
[dayEnd_btnsetTitle:textStr forState:UIControlStateSelected];
[dayEnd_btnsetTitle:textStr forState:UIControlStateNormal];
}
if (monthStr.length >0) {
if (monthStr.length ==2) {
monthStr = [NSString stringWithFormat:@"%02d",[[monthStr substringToIndex:1] intValue]];
}else{
monthStr = [monthStr substringToIndex:2];
}
NSString * textStr = [dayEnd_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(5,2) withString:monthStr];
[dayEnd_btnsetTitle:textStr forState:UIControlStateSelected];
[dayEnd_btnsetTitle:textStr forState:UIControlStateNormal];
}
if (dayStr.length >0) {
if (dayStr.length ==2) {
dayStr = [NSStringstringWithFormat:@"%02d",[[dayStrsubstringToIndex:1]intValue]];
}else{
dayStr = [dayStr substringToIndex:2];
}
NSString * textStr = [dayEnd_btn.titleLabel.textstringByReplacingCharactersInRange:NSMakeRange(8,2) withString:dayStr];
[dayEnd_btnsetTitle:textStr forState:UIControlStateSelected];
[dayEnd_btnsetTitle:textStr forState:UIControlStateNormal];
}
}
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
@end
- iOS 时间选择器 支持月 日
- ACCESS支持的时间日期函数2007年01月10日 16:01ACCESS支持的时间日期函数
- iOS 时间选择器
- iOS时间选择器
- Android 日期选择器隐藏年,月,日
- 时间戳转年月日
- 日、周、月时间控件
- js时间转年月日
- ios根据日期获取当前年,月,日的起始时间和终止时间
- iOS时间戳---获取近n个月(年,日)格式化时间的方法
- iOS出生日期时间选择器、倒计时,籍贯选择器
- iOS 获取 年 月 日
- iOS 开发时间选择器datePicker
- iOS - UITextFieldKeyBoardPan为时间选择器
- iOS 时间/ 日期选择器 UIDatePicker
- 仿ios的时间选择器
- 精确到年、月的时间选择器
- iOS-获取当前时间的年、月、日、时、分、秒
- HDOJ1076 An Easy Task
- Drools7.5.0教程(9)-workbench中git使用
- C++ explicit关键字详解
- mac mysql 登入报错 解决办法
- canvas马赛克
- iOS 时间选择器 支持月 日
- 错误代码: 1052 Column 'sys_date' in where clause is ambiguous
- eclipse集成maven和scala时缺少jar包的buildpath问题
- 增大oracle数据库进程数ORA-12519: TNS:no app
- Gson 反序列化导致成员变量声明时赋值失败
- postgresql通过创建规则(RULE)实现表记录
- js二维数组
- A系统测试工作总结
- C\C++ 中的绝对值函数:abs()、cabs()、fabs()、labs()