1. 程式人生 > >iOS 使用UIPickerView三級聯動實現選擇日期年月日

iOS 使用UIPickerView三級聯動實現選擇日期年月日

選擇時間

程式碼裡面有些自定義的巨集,如果想要這個效果的朋友,不妨花幾分鐘把裡面的巨集改下等不報錯了,直接用這下面的程式碼呼叫就可以了!

呼叫程式碼

 SelectTimeV *selectTimeV = [[SelectTimeV alloc] init];

        selectTimeV.block = ^(NSString *timeStr) {

            if (timeStr) {

                [endTimeLabelV setRightLabelText:timeStr];
            }

        };
        [[UIApplication sharedApplication].keyWindow addSubview:selectTimeV];

.h檔案

#import <UIKit/UIKit.h>

@interface SelectTimeV : UIView

@property (nonatomic, copy) void (^block)(NSString *);

@end

.m檔案

#import "SelectTimeV.h"

@interface SelectTimeV ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

{
    NSInteger yearIndex;

    NSInteger
monthIndex; NSInteger dayIndex; UIView *topV; } @property (nonatomic, strong) UIPickerView *pickerView; @property (nonatomic, strong) NSMutableArray *yearArray; @property (nonatomic, strong) NSMutableArray *monthArray; @property (nonatomic, strong) NSMutableArray *dayArray; @end @implementation
SelectTimeV
- (NSMutableArray *)yearArray { if (_yearArray == nil) { _yearArray = [NSMutableArray array]; for (int year = 2000; year < 2050; year++) { NSString *str = [NSString stringWithFormat:@"%d年", year]; [_yearArray addObject:str]; } } return _yearArray; } - (NSMutableArray *)monthArray { if (_monthArray == nil) { _monthArray = [NSMutableArray array]; for (int month = 1; month <= 12; month++) { NSString *str = [NSString stringWithFormat:@"%02d月", month]; [_monthArray addObject:str]; } } return _monthArray; } - (NSMutableArray *)dayArray { if (_dayArray == nil) { _dayArray = [NSMutableArray array]; for (int day = 1; day <= 31; day++) { NSString *str = [NSString stringWithFormat:@"%02d日", day]; [_dayArray addObject:str]; } } return _dayArray; } - (instancetype)init { self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.frame]; if (self) { self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3]; topV = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, screenWidth, aspectRatio(40))]; topV.backgroundColor = RGBACOLOR(242, 242, 242, 1.0); [self addSubview:topV]; UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; cancelBtn.frame = CGRectMake(0, 0, aspectRatio(100), aspectRatio(40)); [cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [cancelBtn setTitleColor:blackFontColor forState:UIControlStateNormal]; [cancelBtn.titleLabel setFont:regularFontWithSize(16)]; [topV addSubview:cancelBtn]; UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom]; yesBtn.frame = CGRectMake(screenWidth - aspectRatio(100), 0, aspectRatio(100), aspectRatio(40)); [yesBtn setTitle:@"完成" forState:UIControlStateNormal]; [yesBtn setTitleColor:naviBarColor forState:UIControlStateNormal]; [yesBtn.titleLabel setFont:regularFontWithSize(16)]; [topV addSubview:yesBtn]; [cancelBtn click:^(UIView *view) { if (_block) { _block(nil); } [self remove]; }]; [yesBtn click:^(UIView *view) { if (_block) { NSString *timeStr = [NSString stringWithFormat:@"%@%@%@",((UILabel *)[_pickerView viewForRow:yearIndex forComponent:0]).text, ((UILabel *)[_pickerView viewForRow:monthIndex forComponent:1]).text, ((UILabel *)[_pickerView viewForRow:dayIndex forComponent:2]).text]; timeStr = [timeStr stringByReplacingOccurrencesOfString:@"年" withString:@"/"]; timeStr = [timeStr stringByReplacingOccurrencesOfString:@"月" withString:@"/"]; timeStr = [timeStr stringByReplacingOccurrencesOfString:@"日" withString:@""]; _block(timeStr); } [self remove]; }]; [self click:^(UIView *view) { if (_block) { _block(nil); } [self remove]; }]; _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, topV.bottom, screenWidth, aspectRatio(207))]; _pickerView.dataSource = self; _pickerView.delegate = self; _pickerView.backgroundColor = [UIColor whiteColor]; [self addSubview:_pickerView]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 定義一個時間欄位的旗標,指定將會獲取指定年、月、日、時、分、秒的資訊 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday; // 獲取不同時間欄位的資訊 NSDateComponents *comp = [calendar components: unitFlags fromDate:[NSDate date]]; yearIndex = [self.yearArray indexOfObject:[NSString stringWithFormat:@"%ld年", comp.year]]; monthIndex = [self.monthArray indexOfObject:[NSString stringWithFormat:@"%02ld月", comp.month]]; dayIndex = [self.dayArray indexOfObject:[NSString stringWithFormat:@"%02ld日", comp.day]]; [_pickerView selectRow:yearIndex inComponent:0 animated:YES]; [_pickerView selectRow:monthIndex inComponent:1 animated:YES]; [_pickerView selectRow:dayIndex inComponent:2 animated:YES]; [self pickerView:_pickerView didSelectRow:yearIndex inComponent:0]; [self pickerView:_pickerView didSelectRow:monthIndex inComponent:1]; [self pickerView:_pickerView didSelectRow:dayIndex inComponent:2]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UILabel *label = (UILabel *)[_pickerView viewForRow:yearIndex forComponent:0]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); label = (UILabel *)[_pickerView viewForRow:monthIndex forComponent:1]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); label = (UILabel *)[_pickerView viewForRow:dayIndex forComponent:2]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); }); [UIView animateWithDuration:0.25 animations:^{ topV.frame = CGRectMake(0, screenHeight - aspectRatio(247), screenWidth, aspectRatio(40)); _pickerView.frame = CGRectMake(0, topV.bottom, screenWidth, aspectRatio(207)); }]; } return self; } #pragma mark -UIPickerView #pragma mark UIPickerView的資料來源 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return self.yearArray.count; }else if(component == 1) { return self.monthArray.count; }else { switch (monthIndex + 1) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; default: return 28; } } } - (void)remove { [UIView animateWithDuration:0.25 animations:^{ topV.frame = CGRectMake(0, screenHeight, screenWidth, aspectRatio(40)); _pickerView.frame = CGRectMake(0, topV.bottom, screenWidth, aspectRatio(207)); } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } #pragma mark -UIPickerView的代理 // 滾動UIPickerView就會呼叫 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { yearIndex = row; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); }); }else if (component == 1) { monthIndex = row; [pickerView reloadComponent:2]; if (monthIndex + 1 == 4 || monthIndex + 1 == 6 || monthIndex + 1 == 9 || monthIndex + 1 == 11) { if (dayIndex + 1 == 31) { dayIndex--; } }else if (monthIndex + 1 == 2) { if (dayIndex + 1 > 28) { dayIndex = 27; } } [pickerView selectRow:dayIndex inComponent:2 animated:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); label = (UILabel *)[pickerView viewForRow:dayIndex forComponent:2]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); }); }else { dayIndex = row; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component]; label.textColor = RGBACOLOR(26, 174, 135, 1.0); label.font = regularFontWithSize(16); }); } } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { // //設定分割線的顏色 // for(UIView *singleLine in pickerView.subviews) // { // if (singleLine.frame.size.height < 1) // { // singleLine.backgroundColor = kSingleLineColor; // } // } //設定文字的屬性 UILabel *genderLabel = [[UILabel alloc] init]; genderLabel.textAlignment = NSTextAlignmentCenter; genderLabel.textColor = RGBACOLOR(153, 153, 153, 1.0); genderLabel.font = regularFontWithSize(14); if (component == 0) { genderLabel.text = self.yearArray[row]; }else if (component == 1) { genderLabel.text = self.monthArray[row]; }else { genderLabel.text = self.dayArray[row]; } return genderLabel; } @end