/** 初始化pickerView */-(void)initPickerView{ int x_hour = 50; int height_hour = 120; int toolBar_height = 40; float width_hour = MainScreen_Width/2 - x_hour; float y_hour = MainScreen_Height/2 - height_hour - toolBar_height; self.hourPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(x_hour, y_hour, width_hour, height_hour)]; self.hourPickerView.dataSource = self; self.hourPickerView.delegate = self; self.hourPickerView.backgroundColor = [UIColor whiteColor]; [self addSubview:self.hourPickerView]; self.minutesPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(x_hour+width_hour, y_hour, width_hour, height_hour)]; self.minutesPickerView.dataSource = self; self.minutesPickerView.delegate = self; self.minutesPickerView.backgroundColor = [UIColor whiteColor]; [self addSubview:self.minutesPickerView]; UIView *toolBarView = [[UIView alloc] initWithFrame:CGRectMake(x_hour,self.hourPickerView.frame.size.height+self.hourPickerView.frame.origin.y, 2*width_hour, toolBar_height)]; // toolBarView.backgroundColor = [UIColor orangeColor]; [self addSubview:toolBarView]; NSArray *btnTitleArr = [NSArray arrayWithObjects:@"取消",@"確定", nil]; for(int i=0; i < [btnTitleArr count]; i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(i*toolBarView.frame.size.width/2,0, toolBarView.frame.size.width/2, toolBarView.frame.size.height); [btn setTitle:btnTitleArr[i] forState:UIControlStateNormal]; [btn setBackgroundColor:[UIColor orangeColor]]; [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; btn.tag = 100+i; [toolBarView addSubview:btn]; if(i == 1) { UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.origin.x,btn.frame.origin.y, 0.5, btn.frame.size.height)]; lineView.backgroundColor = [UIColor grayColor]; [toolBarView addSubview:lineView]; } } //設定預設選擇目前時間 [self.hourPickerView selectRow:currentHour inComponent:0 animated:YES]; [self.minutesPickerView selectRow:3 inComponent:0 animated:YES];}/** 初始化資料來源 */-(void)_setArr{ hourArr = [NSMutableArray array]; mintuesArr = [NSMutableArray array]; currentHour = [[NSDate date] getHour]; timeSelect = [NSString stringWithFormat:@"%02d:%02d",[[NSDate date] getHour],30]; for(int i=0; i<24; i++) { [hourArr addObject:[NSString stringWithFormat:@"%d",i]]; } for(int i=0; i<6; i++) { [mintuesArr addObject:[NSString stringWithFormat:@"%d",i*10]]; }}-(void)btnAction:(UIButton *)sender{ switch (sender.tag) { case 100://取消 { if(self.hidePickerView) { self.hidePickerView(sender.tag,@""); } } break; case 101://確定 { if(self.hidePickerView) { self.hidePickerView(sender.tag,timeSelect); } } break; default: break; }}#pragma mark - UIPickerViewDataSource- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if(pickerView == self.hourPickerView) { return [hourArr count]; } else { return [mintuesArr count]; }}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if(pickerView == self.hourPickerView) { selectHour = [NSString stringWithFormat:@"%02d",[hourArr[row] intValue]]; } else if(pickerView == self.minutesPickerView) { selectMin = [NSString stringWithFormat:@"%02d",[mintuesArr[row] intValue]]; } timeSelect = [NSString stringWithFormat:@"%@:%@",selectHour,selectMin];}#pragma makr - UIPickerViewDelegate- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)componen{ if(pickerView == self.hourPickerView) { return [hourArr objectAtIndex:row]; } else { return [mintuesArr objectAtIndex:row]; }}- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated{ }
使用方法:
CustomDatePickView *datepickView = [[CustomDatePickView alloc] initWithFrame:self.view.bounds]; //通過block響應呼叫事件和接受選擇的時間值 __weak CustomDatePickView *wDatePickerView = datepickView; datepickView.hidePickerView = ^(NSInteger senderTag,NSString *selectTime){ switch (senderTag) { case 100: { [wDatePickerView removeFromSuperview]; } break; case 101: { [wDatePickerView removeFromSuperview]; selectTimeStr = selectTime; NSString *selectDate = @""; for(UIButton *btn in self.storedBtnView.subviews) { if(btn.selected) { switch (btn.tag) { case 100://今天 { selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"year"] intValue],[dateDic[@"month"] intValue],[dateDic[@"day"] intValue],[self _getWeakDay:btn],selectTime]; [self updateBookingBtnUI:selectDate]; } break; case 101://明天 { selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"nextYear"] intValue],[dateDic[@"tomorrowMonth"] intValue],[dateDic[@"tomorrow"] intValue],[self _getWeakDay:btn],selectTime]; [self updateBookingBtnUI:selectDate]; } break; case 102://後天 { selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"lastYear"] intValue],[dateDic[@"afterDayMonth"] intValue],[dateDic[@"afterDay"] intValue],[self _getWeakDay:btn],selectTime]; [self updateBookingBtnUI:selectDate]; } break; default: break; } } } } break; default: break; } }; [self.view addSubview:datepickView];