用显示键盘动画的方式来显示DatePicker
1.显示DatePicker
[plain]
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath]; self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text]; // 查看DatePicker是否显示在屏幕上 if (self.pickerView.superview == nil) {
[plain]
// 添加选取器到屏幕上
[plain]
[self.view.window addSubview: self.pickerView]; // size up the picker view to our screen and compute the start/end frame origin for our slide up animation // // 计算DatePicker初始的Frame CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero]; CGRect startRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height, pickerSize.width, pickerSize.height); self.pickerView.frame = startRect; // compute the end frame CGRect pickerRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height - pickerSize.height, pickerSize.width, pickerSize.height); // start the slide up animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // we need to perform some post operations after the animation is complete [UIView setAnimationDelegate:self]; self.pickerView.frame = pickerRect; // shrink the table vertical size to make room for the date picker CGRect newFrame = self.tableView.frame; newFrame.size.height -= self.pickerView.frame.size.height; self.tableView.frame = newFrame; [UIView commitAnimations]; // add the "Done" button to the nav bar self.navigationItem.rightBarButtonItem = self.doneButton; } 2.消除DatePicker