有起始時間或者截止時間的日期選擇器 iOS

最近公司項目篩選話題里有個日期范圍選擇,考慮到開始時間低于結(jié)束時間,就寫了個有時間范圍的日期選擇器,僅供參考:

選擇時間.jpg

點擊選擇結(jié)束時間 --- 以起始時間作為節(jié)點.jpg

點擊選擇起始時間 --- 以結(jié)束時間作為節(jié)點.jpg

關(guān)于pickerView相關(guān)的代碼

#pragma mark - --- delegate 視圖委托 ---

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        if (self.beginDate) {
            return 2100 - _year;
        }
        if (self.endDate) {
            return _year + 1 - self.yearMin ;
        }
        return yearSum;
    }else if(component == 1) {
        if (self.beginDate) {//有起始時間的情況下
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
            if (yearSelected == [NSCalendar currentYear:self.beginDate]) {
                return 13 - [NSCalendar currentMonth:self.beginDate];
            } else {
                return 12;
            }
        }
        if (self.endDate) {//有結(jié)束時間的情況下
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
            if (yearSelected == [NSCalendar currentYear:self.endDate]) {
                return [NSCalendar currentMonth:self.endDate];
            } else {
                return 12;
            }
        }
        return 12;
    }else {
        if (self.beginDate) {//有起始時間的情況下
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
            if (yearSelected == [NSCalendar currentYear:self.beginDate]) {
                NSInteger monthSelected = [pickerView selectedRowInComponent:1] + [NSCalendar currentMonth:self.beginDate];
                if ([pickerView selectedRowInComponent:1] == 0) {
                    return [NSCalendar getDaysWithYear:yearSelected month:monthSelected] - [NSCalendar currentDay:self.beginDate] + 1;
                } else {
                    return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
                }
            } else {
                NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
                return  [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
            }
        }
        if (self.endDate) {//有結(jié)束時間的情況下
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
            if (yearSelected == [NSCalendar currentYear:self.endDate]) {
                NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
                if (monthSelected == [NSCalendar currentMonth:self.endDate]) {
                    return [NSCalendar currentDay:self.endDate];
                } else {
                    return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
                }
            } else {
                NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
                return  [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
            }
        }
        NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
        NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
        return  [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
    }
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return PickerViewLabelWeight;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
            [pickerView selectRow:(12 - [NSCalendar currentMonth:self.beginDate]) inComponent:1 animated:NO];
            [pickerView selectRow:0 inComponent:1 animated:NO];
            [pickerView selectRow:(28 - [NSCalendar currentDay:self.beginDate]) inComponent:2 animated:NO];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            self.year  = [self.pickerView selectedRowInComponent:0] + self.yearMin;
            if (self.beginDate) {
                if (self.year == self.yearMin) {
                    self.month = [NSCalendar currentMonth:self.beginDate];
                    self.day = [NSCalendar currentDay:self.beginDate];
                } else {
                    self.month = 1;
                    self.day = 1;
                }
            } else {
                self.month = 1;
                self.day = 1;
            }
            break;
        case 1:
            [pickerView reloadComponent:2];
            [pickerView selectRow:(28 - [NSCalendar currentDay:self.beginDate]) inComponent:2 animated:NO];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            if (self.beginDate) {
                if (self.year == self.yearMin) {
                    self.month = [self.pickerView selectedRowInComponent:1] + [NSCalendar currentMonth:self.beginDate];
                    if ([self.pickerView selectedRowInComponent:1] == 0) {
                        self.day = [NSCalendar currentDay:self.beginDate];
                    } else {
                        self.day = 1;
                    }
                } else {
                    self.month = [self.pickerView selectedRowInComponent:1] + 1;
                    self.day = 1;
                }
            } else {
                self.month = [self.pickerView selectedRowInComponent:1] + 1;
                self.day = 1;
            }
        default:
            if (self.beginDate) {
                if (self.year == self.yearMin) {
                    if (self.month == [NSCalendar currentMonth:self.beginDate]) {
                        self.day = [self.pickerView selectedRowInComponent:2] + [NSCalendar currentDay:self.beginDate];
                    } else {
                        self.day = [self.pickerView selectedRowInComponent:2] + 1;
                    }
                } else {
                    self.day = [self.pickerView selectedRowInComponent:2] + 1;
                }
            }else {
                self.day = [self.pickerView selectedRowInComponent:2] + 1;
            }
            break;
    }
    
    [self reloadData];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    NSString *text;
    if (component == 0) {
        if (self.beginDate) {
            text =  [NSString stringWithFormat:@"%zd", row + [NSCalendar currentYear:self.beginDate]];
        } else {
            text =  [NSString stringWithFormat:@"%zd", row + self.yearMin ];
        }
    }else if (component == 1){
        if (self.beginDate) {
            if (self.year == self.yearMin ) {
                text = [NSString stringWithFormat:@"%zd", row + [NSCalendar currentMonth:self.beginDate]];
            } else {
                text = [NSString stringWithFormat:@"%zd", row + 1];
            }
        } else {
            text = [NSString stringWithFormat:@"%zd", row + 1];
        }
    }else{
        if (self.beginDate) {
            if (self.year == self.yearMin ) {
                if (self.month == [NSCalendar currentMonth:self.beginDate]) {
                    text = [NSString stringWithFormat:@"%zd", row + [NSCalendar currentDay:self.beginDate]];
                } else {
                    text = [NSString stringWithFormat:@"%zd", row + 1];
                }
            } else {
                text = [NSString stringWithFormat:@"%zd", row + 1];
            }
        } else {
            text = [NSString stringWithFormat:@"%zd", row + 1];
        }
    }

    UILabel *label = [[UILabel alloc]init];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont systemFontOfSize:17]];
    [label setText:text];
    return label;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,939評論 25 709
  • 難得一身輕松行, 看看風(fēng)景散散心。 吃好喝好休息好, 沿途不忙發(fā)微信。
    石三英語閱讀 232評論 0 0
  • 不為輕眸苦思念,上眼彎弦舊夢人。夜!思戀的開始夢的終結(jié)。閉眼是你的影,睜眼是一堆的愁,是說不清的由來已久。 開始遂...
    月流流梭閱讀 185評論 0 0
  • //聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄 一、獲取系統(tǒng)所支持的國際化信息 //iPhone中...
    石虎132閱讀 426評論 0 6
  • 偶然抬眼間,看到一樹的驚喜,滿滿的花兒掛在那里,蓬勃而熱烈。
    阿正_fz閱讀 241評論 0 0

友情鏈接更多精彩內(nèi)容