在平時(shí)的開(kāi)發(fā)中椿浓,經(jīng)常會(huì)用到日期選擇器凑兰,但如果用的是系統(tǒng)提供的UIDatePicker控件闹啦,有時(shí)無(wú)法滿足自己的需求(當(dāng)然有時(shí)覺(jué)得丑)挠进,所以我們可以通過(guò)UIPickerView來(lái)自定義一個(gè)日期選擇器色乾。
下面我寫(xiě)了兩個(gè)經(jīng)常會(huì)用的日期選擇器:
- 通用日期選擇器
- 有起始、截止范圍的日期選擇器(聯(lián)動(dòng)日期選擇器--實(shí)在想不出叫啥名字了领突。暖璧。。)
效果圖如下:
通用日期選擇器
聯(lián)動(dòng)日期選擇器
文件在工程中的位置:
調(diào)用方法:
- 通用日期選擇器調(diào)用方法君旦。若只需默認(rèn)值澎办,則只需把隱藏的pickerview顯示出來(lái)即可而無(wú)需調(diào)用下面的方法(self.pickerView.hidden = NO;)。
/**
配置日期選擇器
@param selectedDate 默認(rèn)選擇日期(必須在最大日期和最小日期范圍)金砍,若nil局蚀,則為當(dāng)前時(shí)間
@param dateFormatter 日期格式,若nil恕稠,則為yyyy-MM-dd
@param datePickerMode 日期選擇器組件類型琅绅,默認(rèn)為SLDatePickerModeDate
*/
- (void)setupPickerViewDataWithDefaultSelectedDate:(NSDate *)selectedDate
dateFormatter:(NSString *)dateFormatter
datePickerMode:(SLDatePickerMode)datePickerMode;
示例代碼:
//如果只需要默認(rèn)值,則屏蔽這行代碼
[self.pickerView setupPickerViewDataWithDefaultSelectedDate:[NSDate date] dateFormatter:@"yyyy-MM-dd" datePickerMode:SLDatePickerModeDate];
- 聯(lián)動(dòng)日期選擇器調(diào)用方法
/**
配置日期選擇器
@param selectedDate 默認(rèn)選擇日期(必須在最大日期和最小日期范圍)鹅巍,若nil千扶,則為當(dāng)前時(shí)間
@param maximumDate 最大日期料祠,若nil,則為[NSDate distantFuture]
@param minimumDate 最小日期县貌,若nil术陶,則為1970-01-01
@param dateFormatter 日期格式,若nil煤痕,則為yyyy-MM-dd
@param datePickerMode 日期選擇器組件類型梧宫,默認(rèn)為SLDatePickerModeDate
*/
- (void)setupPickerViewDataWithDefaultSelectedDate:(NSDate *)selectedDate
maximumDate:(NSDate *)maximumDate
minimumDate:(NSDate *)minimumDate
dateFormatter:(NSString *)dateFormatter
datePickerMode:(SLDatePickerMode)datePickerMode;
示例代碼:
[self.associatedPickerView setupPickerViewDataWithDefaultSelectedDate:[[NSDate date] dateBySubtractingYears:1]
maximumDate:[NSDate date]
minimumDate:[[NSDate date] dateBySubtractingYears:2]
dateFormatter:@"yyyy-MM"
datePickerMode:SLDatePickerModeYearAndMonth];
核心代碼實(shí)現(xiàn):
- 通用日期選擇器代碼實(shí)現(xiàn)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (component) {
case 0: {
self.selectedYear = row % self.yearRows + self.minimumDate.year;
if (SLDatePickerModeYearAndMonth == self.datePickerMode
|| SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedMonth:self.selectedMonth];
if (SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedDay:self.selectedDay];
}
}
}
break;
case 1: {
self.selectedMonth = row % self.monthRows + 1;
if (SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedDay:self.selectedDay];
}
}
break;
case 2: {
self.selectedDay = row % self.dayRows + 1;
}
break;
}
[pickerView reloadAllComponents];
}
#pragma mark - Setter
- (void)setSelectedYear:(NSInteger)selectedYear {
self.yearRows = self.maximumDate.year - self.minimumDate.year + 1;
selectedYear = (selectedYear > self.maximumDate.year) ? self.maximumDate.year : selectedYear;
selectedYear = (selectedYear < self.minimumDate.year) ? self.minimumDate.year : selectedYear;
_selectedYear = selectedYear;
//每次選擇都在中間位置附近,保證看似無(wú)限滾動(dòng)的效果
NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.yearRows) * self.yearRows + _selectedYear - self.minimumDate.year;
if (self.selectedRows.count > 0) {
[self.selectedRows replaceObjectAtIndex:0 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:0];
[self selectRow:selectedRow inComponent:0 animated:NO];
}
- (void)setSelectedMonth:(NSInteger)selectedMonth {
_selectedMonth = selectedMonth;
self.monthRows = 12;
//每次選擇都在中間位置附近摆碉,保證看似無(wú)限滾動(dòng)的效果
NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.monthRows) * self.monthRows + _selectedMonth - 1;
if (self.selectedRows.count > 1) {
[self.selectedRows replaceObjectAtIndex:1 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:1];
[self selectRow:selectedRow inComponent:1 animated:NO];
}
- (void)setSelectedDay:(NSInteger)selectedDay {
self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
_selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
//每次選擇都在中間位置附近塘匣,保證看似無(wú)限滾動(dòng)的效果
NSInteger selectedRow = ((kMaxNumberOfRows / 2) / self.dayRows) * self.dayRows + _selectedDay - 1;
if (self.selectedRows.count > 2) {
[self.selectedRows replaceObjectAtIndex:2 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:2];
[self selectRow:selectedRow inComponent:2 animated:NO];
}
- 聯(lián)動(dòng)日期選擇器代碼實(shí)現(xiàn)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (component) {
case 0: {
self.selectedYear = row % self.yearRows + self.minimumDate.year;
if (SLDatePickerModeYearAndMonth == self.datePickerMode
|| SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedMonth:self.selectedMonth];
if (SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedDay:self.selectedDay];
}
}
}
break;
case 1: {
if (self.selectedYear == self.minimumDate.year) {
self.selectedMonth = row % self.monthRows + self.minimumDate.month;
} else {
self.selectedMonth = row % self.monthRows + 1;
}
if (SLDatePickerModeDate == self.datePickerMode) {
[self setSelectedDay:self.selectedDay];
}
}
break;
case 2: {
if ([self.maximumDate isSameYearAsDate:self.minimumDate]
&& [self.maximumDate isSameMonthAsDate:self.minimumDate]) {
self.selectedDay = row % self.dayRows + self.minimumDate.day;
} else if (self.selectedYear == self.minimumDate.year) {
if (self.selectedMonth == self.minimumDate.month) {
self.selectedDay = row % self.dayRows + self.minimumDate.day;
} else {
self.selectedDay = row % self.dayRows + 1;
}
} else {
self.selectedDay = row % self.dayRows + 1;
}
}
break;
}
[pickerView reloadAllComponents];
}
#pragma mark - Setter
- (void)setSelectedYear:(NSInteger)selectedYear {
self.yearRows = self.maximumDate.year - self.minimumDate.year + 1;
selectedYear = (selectedYear > self.maximumDate.year) ? self.maximumDate.year : selectedYear;
selectedYear = (selectedYear < self.minimumDate.year) ? self.minimumDate.year : selectedYear;
_selectedYear = selectedYear;
NSInteger selectedRow = (_selectedYear - self.minimumDate.year) % self.yearRows;
if (self.selectedRows.count > 0) {
[self.selectedRows replaceObjectAtIndex:0 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:0];
[self selectRow:selectedRow inComponent:0 animated:YES];
}
- (void)setSelectedMonth:(NSInteger)selectedMonth {
NSInteger selectedRow = selectedMonth - 1;
if ([self.maximumDate isSameYearAsDate:self.minimumDate]) {
self.monthRows = self.maximumDate.month - self.minimumDate.month + 1;
selectedMonth = (selectedMonth > self.maximumDate.month) ? self.maximumDate.month : selectedMonth;
selectedRow = (selectedMonth - self.minimumDate.month) % self.monthRows;
} else if (self.selectedYear == self.maximumDate.year) {
self.monthRows = self.maximumDate.month;
selectedMonth = (selectedMonth > self.monthRows) ? self.monthRows : selectedMonth;
selectedRow = (selectedMonth - 1) % self.monthRows;
} else if (self.selectedYear == self.minimumDate.year) {
self.monthRows = 12 - self.minimumDate.month + 1;
selectedMonth = (selectedMonth < self.minimumDate.month) ? self.minimumDate.month : selectedMonth;
selectedRow = (selectedMonth - self.minimumDate.month) % self.monthRows;
} else {
self.monthRows = 12;
}
_selectedMonth = selectedMonth;
if (self.selectedRows.count > 1) {
[self.selectedRows replaceObjectAtIndex:1 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:1];
[self selectRow:selectedRow inComponent:1 animated:YES];
}
- (void)setSelectedDay:(NSInteger)selectedDay {
NSInteger selectedRow = 0;
if ([self.maximumDate isSameYearAsDate:self.minimumDate]
&& [self.maximumDate isSameMonthAsDate:self.minimumDate]) {
self.dayRows = self.maximumDate.day - self.minimumDate.day + 1;
selectedDay = (selectedDay > self.maximumDate.day) ? self.maximumDate.day : selectedDay;
selectedRow = (selectedDay - self.minimumDate.day) % self.dayRows;
} else if (self.selectedYear == self.maximumDate.year) {
if (self.selectedMonth == self.maximumDate.month) {
self.dayRows = self.maximumDate.day;
} else {
self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
}
selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
selectedRow = (selectedDay - 1) % self.dayRows;
} else if (self.selectedYear == self.minimumDate.year) {
//當(dāng)前選擇年、月下的天數(shù)
NSInteger days = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
if (self.selectedMonth == self.minimumDate.month) {
//需要顯示天的行數(shù):(選擇月所有天數(shù)的和 - 指定最小日期的天數(shù) + 1)巷帝。
//例:8月份有31天忌卤,若最小天為13號(hào),則該顯示的行數(shù)為:31 - 13 + 1 = 19行(從13到31)
self.dayRows = days - self.minimumDate.day + 1;
if (selectedDay > days) {//如果選中天數(shù)大于當(dāng)月最大天數(shù)
selectedDay = days;
}
selectedDay = (selectedDay < self.minimumDate.day) ? self.minimumDate.day : selectedDay;
selectedRow = (selectedDay - self.minimumDate.day) % self.dayRows;
} else {
self.dayRows = days;
selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
selectedRow = (selectedDay - 1) % self.dayRows;
}
} else {
self.dayRows = [NSDate getDaysWithYear:self.selectedYear month:self.selectedMonth];
selectedDay = (selectedDay > self.dayRows) ? self.dayRows : selectedDay;
selectedRow = (selectedDay - 1) % self.dayRows;
}
_selectedDay = selectedDay;
if (self.selectedRows.count > 2) {
[self.selectedRows replaceObjectAtIndex:2 withObject:@(selectedRow)];
} else {
[self.selectedRows addObject:@(selectedRow)];
}
[self reloadComponent:2];
[self selectRow:selectedRow inComponent:2 animated:YES];
}
好了楞泼,代碼就貼這么多了驰徊,如果有需要的朋友可以到github上面去下載。
里面有部分代碼用到 NSDate+Category堕阔, 可自行查看棍厂。